[英]How to find the smallest four digit number, whose digits dont repeat, but add up to a randomized number
I need to take a number, lets say 6, and then find the smallest 4 digit number, whose digits do not repeat and add up to 6.我需要取一个数字,比如说 6,然后找到最小的 4 位数字,其数字不重复并且加起来为 6。
For example(These will not add up to the same number):例如(这些不会加起来相同的数字):
1023
3045
2345
These numbers are all ok because their digits do not repeat and are four digits这些数字都可以,因为它们的数字不重复并且是四位数
While:尽管:
1122
3344
123
These are not ok, because they either are not four digits or their numbers repeat这些都不行,因为它们要么不是四位数,要么它们的数字重复
I'm currently at a roadblock where I can find said four digit number, but one: it is in a list which the program i need to plug this into wont accept and two: the digits aren't in the same order as the answers on the program (ie the smallest four digit number that does not have repeat digits, but adds up to six is 1023, but my program returns 0123, which is incorret.我目前在一个障碍中,我可以找到所说的四位数字,但一个:它在一个列表中,我需要将其插入的程序不会接受,两个:数字与答案的顺序不同在程序上(即没有重复数字但加起来为六的最小四位数字是 1023,但我的程序返回 0123,这是不正确的。
Here is my current code:这是我当前的代码:
x = 6
Sum = 0
#Find the four digit number
for i in range (999, 10000):
#Find the sum of those numbers
Sum = sum(map(int, str(i)))
#Check if sum is = to x
if Sum == x:
num = i
#Get rid of any identical numbers
result = list(set(map(int, str(num))))
#Make sure the length is 4
if len(result) == 4:
print(result)
#Output [0,1,2,3]
Any help on how I could change this to work for what I want would be great关于我如何改变它以达到我想要的目的的任何帮助都会很棒
I've changed your program to print i
instead of result
, and break
out of the loop when it finds the first number (which logically must be the smallest):我已经改变了你的程序打印
i
的,而不是result
,并break
循环出来的时候发现的第一个数字(这在逻辑上必须是最小的):
x = 6
Sum = 0
#Find the four digit number
for i in range (999, 10000):
#Find the sum of those numbers
Sum = sum(map(int, str(i)))
#Check if sum is = to x
if Sum == x:
num = i
#Get rid of any identical numbers
result = list(set(map(int, str(num))))
#Make sure the length is 4
if len(result) == 4:
print(i)
break
This program will print 1023
.该程序将打印
1023
。
You could neaten it up a bit by turning it into a function with x
as a parameter and return
ing the result instead of breaking and printing.您可以通过将其转换为以
x
作为参数的函数并return
结果而不是中断和打印来稍微整理一下。 Also it looks as if the num
variable is redundant, you can just stick with i
.此外看起来好像
num
变量是多余的,你可以坚持使用i
。
Changed your code a little:稍微改变了你的代码:
x = 6
Sum = 0
result={}
#Find the four digit number
for i in range (999, 10000):
#Find the sum of those numbers
Sum = sum(map(int, str(i)))
#Check if sum is = to x
if Sum == x:
num = i
aux = ''.join(list(set(map(str, str(num)))))
if not aux in result:
result[aux] = []
result[aux].append(i)
for k in result:
print(k, min(result[k]))
Using recursive algorithm:使用递归算法:
def get_num(n, s, v=""):
for el in range(1 if v=="" else 0, 10):
if str(el) not in v:
temp_sum=sum(int(l) for l in v+str(el))
if(temp_sum>s):
break
elif len(v)==n-1:
if(temp_sum==s):
return v+str(el)
else:
ret=get_num(n, s, v+str(el))
if(ret): return ret
Outputs:输出:
print(get_num(4,6))
>> 1023
You might be interested in itertools
package which is designed for solving combinatoric problems like yours.您可能对
itertools
包感兴趣,它旨在解决像您这样的组合问题。
for n in combinations(range(10),4):
if sum(n)==6 and n[0]!=0:
print(n)
break
combinations
method used here returns a generator of all tuples of length 4 that contains distinct digits from 0 to 9, they are sorted alphabetically.此处使用的
combinations
方法返回长度为 4 的所有元组的生成器,其中包含从 0 到 9 的不同数字,它们按字母顺序排序。
You need to use from itertools import combinations
to make it work.您需要使用
from itertools import combinations
才能使其工作。
Since you need the smallest integer, you can stop the search at the first encountered with a break
:由于您需要最小的整数,您可以在第一个遇到
break
停止搜索:
sum=6 # The sum of digits required.
found=0
for i in range(1000, 10000): # i from 1000 to 9999.
a=i%10;
b=i//10%10
c=i//100%10
d=i//1000%10
if (a!=b)&(a!=c)&(a!=d)&(b!=c)&(b!=d)&(c!=d)&(a+b+c+d==sum):
found=True
break
if found:
print(i)
else:
print('Not found such a number.')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.