简体   繁体   English

从其他数字的最小数字创建数字

[英]Creating a number from smallest digits of other numbers

I'm looking for a way to create a number which is composed from the smallest digits of other numbers.我正在寻找一种方法来创建一个由其他数字的最小数字组成的数字。 It starts with an input which directs how many numbers there will be.它从一个输入开始,该输入指示将有多少个数字。 After that the program needs to take input from those numbers and find the smallest digit.之后,程序需要从这些数字中获取输入并找到最小的数字。 In the end it should create a new number which is made from all the smallest numbers one next to another.最后,它应该创建一个新数字,该数字由所有最小的数字组成,一个接一个地排列。 I figured out how to find the smallest digit, but don't know what to do next.我想出了如何找到最小的数字,但不知道下一步该怎么做。

Example:例子:

4 #first number

#4 numbers since the first input equals 4 (each one in its own line)
123456
345345
2423262
644243

1323 #number made from the smallest digits of the 4 numbers above
num=int(input())
smallest=num%10
while num > 0:
    reminder = num % 10
    if smallest > reminder:
        smallest = reminder
    num =int(num / 10)
print("The Smallest Digit is ", smallest) 

If I've understood the question correctly, I believe something like this is what you're after:如果我正确地理解了这个问题,我相信这样的事情就是你所追求的:

def smallest_digit(num):
    smallest = num % 10
    while num > 0:
        reminder = num % 10
        if smallest > reminder:
            smallest = reminder
        num = int(num / 10)
    return smallest

small_num = ""
number_of_inputs = int(input("Number of inputs: "))

for i in range(number_of_inputs):
    small_num += str(smallest_digit(int(input())))

First, the user is asked for the number of numbers they wish to input.首先,要求用户输入他们希望输入的数字的数量。

Then, the for loop will ask the user for an input number for the correct number of times that the user wanted.然后,for 循环将要求用户输入数字,以达到用户想要的正确次数。

This input is passed straight to the function smallest_digit (this is using your code from above to find the smallest digit in a given number).此输入直接传递给 function smallest_digit(这是使用上面的代码来查找给定数字中的最小数字)。

The function returns the smallest digit, which is then added to a string called small_num. function 返回最小的数字,然后将其添加到名为 small_num 的字符串中。

You can do this without converting the numbers into integers.您可以在不将数字转换为整数的情况下执行此操作。

Print smallest number from the small digits in each line从每一行的小数字中打印出最小的数字

n = int(input())
y = [min(input()) for _ in range(n)] 
y.sort()
print (''.join(y))

Input:输入:

4
123456
345345
2423262
644243

Output will be: Output 将是:

1223

This will give you the smallest digit of the smallest digits you found.这将为您提供您找到的最小数字中的最小数字。

If you just want to print the number in the same order, you can exclude the y.sort() line.如果您只想以相同的顺序打印数字,则可以排除 y.sort() 行。

n = int(input())
y = [min(input()) for _ in range(n)] 
print (''.join(y))

Input:输入:

4
123456
345345
2423262
644243

Output: Output:

1322

Wouldn't it just be this (below)?不就是这个(下)吗? I omitted your limitation that num cannot be 0. I didn't understand the purpose of that.我省略了您对 num 不能为 0 的限制。我不明白这样做的目的。

num=int(input())
listOfSmallestDigits = []
i = 0
while i < num:
     smallest=num%10
     remainder = num % 10
     if smallest > reminder:
        smallest = reminder
     print("The Smallest Digit is ", smallest)
     listOfSmallestDigits.append(smallest)

# Create a String Variable to Collect All the New Smallest Digits in a String
newDigit = ""
for digit in listOfSmallestDigits:
  newDigit = newDigit + digit

print("The final number is: {}".format(newDigit)) 

Also, if you want to use the newDigit as a number later on, just use this:此外,如果您想稍后将 newDigit 用作数字,只需使用:

newDigit = int(newDigit)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM