Python - 不使用数组获取第二小的数字

3
我想要打印出第二小的数字,但不使用任何数组,但它不起作用。 迄今为止我所做的就是这样的。
numbers = 5
print('Enter your numbers\n')

for x in range(0, numbers):
    use = input('Enter #' + str(x + 1) + ': ')
    if x == 0:
        small = int(use)
        second = small
    if int(use) < small:
        small = int(use)
        if small > second:
            second = int(use)

print('\nSecond smallest number is ' + str(second))

我会解决它。这需要一些时间,请耐心等待。 - Rishabh Semwal
这个回答解决了你的问题吗?Python - 查找第二小的数字 - Ank
@Ank 不,他们正在使用数组和其他东西。我需要逻辑而不是库。但还是谢谢。 - user15242190
5个回答

1

代码:

numbers = 5
min = 2**32
second_min = 2**32
print('Enter your numbers\n')

for x in range(numbers):
    use = input('Enter #' + str(x + 1) + ': ')
    if int(use) < min:
        min = int(use)
    elif int(use) >= min and int(use) < second_min:
        second_min = int(use)

print('\nSecond smallest number is ' + str(second_min))

result:

Enter your numbers

Enter #1: 5
Enter #2: 6
Enter #3: 7
Enter #4: 8
Enter #5: 9

Second smallest number is 6

如果输入大于2^32怎么办?Python也可以处理大整数。 - Ank
@Ank 这只是一个默认的最小值,如果你需要一个巨大的输入,可以轻松更改它。 - leaf_yakitori
这个有点复杂,但是很有趣。感谢你的帮助。 - user15242190

1
numbers = 5
print('Enter your numbers\n')

small = float('inf')
second = float('inf')

for x in range(0, numbers):
    use = int(input('Enter #' + str(x + 1) + ': '))
    if use < small:
       small = use
    if use > small and use < second:
        second = use

print('\nSecond smallest number is ' + str(second))

附言:float('inf')是最大的数字


0

这个代码不需要使用列表,而且能够处理任意顺序的数字。

numbers = 5
print('Enter your numbers\n')
#Instead of using lists, we'll use variables to store the smallest numbers. We'll give them the initial value of 0.
smallest = 0
secondsmallest = 0
    
for x in range(0, numbers):
    #We get the user input and convert it to int.
    use = int(input('Enter #' + str(x + 1) + ': '))
    #If the smallest variable hasn't been touched, i.e., it's the first time the for loop is run, we assign the current input to be the smallest number.
    if smallest == 0:
        smallest = use
    #Else if the current input is smaller than the current smallest number, then it should be the new smallest number.
    elif use < smallest:
        smallest = use
    #Else if the current input is greater than the smallest and the secondsmallest number still is untouched then the current input should be the secondsmallest.
    elif use > smallest and secondsmallest == 0:
        secondsmallest = use
    #Else if the current input is less than the secondsmallest, then it should be the new secondsmallest.
    elif use < secondsmallest:
        secondsmallest = use
        
print(secondsmallest)

1
我还没有测试过你的代码,但如果它能够正常工作,那么这就是我真正想要的。不使用任何数组/列表/库或其他仅限于Python的解决方案。 - user15242190

0

代码:

nums = [1, 2, 9, 11, 13]
for counter,num in enumerate(nums):
 
    if counter == 0:
        smallest = num
    if counter == 1:
        second = num
    if counter > 1:
        if second == smallest:
            second = num
        if num < second:
            second = num
        if second < smallest:
            smallest, second = second, smallest
                     
print(second)
    

这是我的方法的算法:

  1. 首先将最小值和第二小的数初始化为前两个输入。也就是说,Smallest=First_Inputsecond_smallest = Second_Input。以下代码实现了这一步骤:
    if counter == 0:
        smallest = num
    if counter == 1:
        second = num
  1. 接下来看第三个数字。如果第三个数字比second_smallest小,则交换它们。以下代码实现了这一步骤。
    # This part is executed only when the counter exceeds 1
    # That is, from the third input onwards.
    if num < second:
        second = num

3. 现在看一下最小的数和第二小的数。如果第二小的数比最小的数还要小,那么交换它们。以下代码实现了这个功能:
    if second < smallest:
        smallest, second = second, smallest

现在,对于输入[最小值,最小值,x,y,z]将会有一个边缘情况。即当最小数重复时。这是因为smallestsecond变量分别存储第一和第二个最小的数字。但是当最小数重复时,它们都存储相同的值(即smallest值)。因此,在这种情况下,实际的second_smallest值将无法替换second中的值(因为在这种情况下second存储最小值) 为解决这个问题,我们有以下这行代码:
     if second == smallest:
         second = num

0
我们可以通过使用另一个临时变量来解决这个问题。
假设输入一个变量 - v。
首先,将v与最小数进行比较。
如果v小于最小数,则最小数将为v,第二小的数将为先前的最小数。
其次,如果v等于或大于最小数,则与第二小的数进行比较并处理。
import sys
numbers = 5
second = sys.maxsize
smallest = sys.maxsize

for x in range(0, numbers):
    use = int(input('Enter #' + str(x + 1) + ': '))
    if use < smallest:
      second = smallest
      smallest = use
    elif use < second:
      second = use

print('\nSecond smallest number is ' + str(second))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接