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))
    
3 个评论
我会解决这个问题。这需要一些时间。 请等待
Ank
这是否回答了你的问题? Python - 查找第二小的数字
@Ank 不,他们在使用数组和其他东西。我需要的是逻辑而不是库。但还是要感谢你。
python
python-3.x
user15242190
发布于 2021-05-20
5 个回答
leaf_yakitori
leaf_yakitori
发布于 2021-05-20
0 人赞同

code:

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
    
Ank
如果输入大于 2^32 怎么办? Python也可以处理大整数。
@Ank 这只是一个默认的最小值,如果你需要一个巨大的输入,可以很容易地改变它。
这有点复杂,但很有趣。谢谢你对我的帮助。
Albert
Albert
发布于 2021-05-20
0 人赞同
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))

P.S. float('inf')是最大的数字

Diego Hernandez Herrera
Diego Hernandez Herrera
发布于 2021-05-20
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)
    
我还没有测试过你的代码,但如果它能工作,那么这就是我真正在寻找的东西。在不使用任何数组/列表/库或其他只有Python的解决方案的情况下。
Jdeep
Jdeep
发布于 2021-05-20
0 人赞同

Code:

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)

以下是我的方法的算法。

  • First initialise the smallest and second smallest numbers to be the first two inputs. That is, Smallest=First_Input and second_smallest = Second_Input. These lines do that:
  •     if counter == 0:
            smallest = num
        if counter == 1:
            second = num
    
  • Next See the third number. If the third number is smaller than the second_smallest number, then swap them. These lines do that
  •     # This part is executed only when the counter exceeds 1
        # That is, from the third input onwards.
        if num < second:
            second = num
    
  • Now See the smallest and second_smallest number. If second_smallest < smallest , then swap them. These lines do that:
  •     if second < smallest:
            smallest, second = second, smallest
    
  • Now, there will be an edgecase for inputs [smallest, smallest, x, y, z] . That is, when smallest number is repeated. It is because, the smallest and second variables store the first smallest and second smallest numbers respectively. But when the smallest number is repeated, they both store the same value(i.e the smallest value). Therefore in this case, the actual second_smallest value will not be able to replace the value in second(since second stores the smallest in this case) To fix this, We have this line of code :
  •      if second == smallest:
             second = num
        
    Yuanzhe Cui
    Yuanzhe Cui
    发布于 2021-05-20
    0 人赞同

    我们可以通过使用另一个临时变量来解决这个问题。
    让我们假设输入一个变量--v。
    First ,用最小的数字比较v。
    如果v比最小的数字小,最小的数字将是v,第二个小数字将是之前的最小数字。
    Second ,如果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