[英]Python - Get second smallest number without using array
I want to print the second smallest number without using any array but it doesn't work.我想在不使用任何数组的情况下打印第二小的数字,但它不起作用。 This is what I've done so far.
这是我到目前为止所做的。
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))
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
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))
PS float('inf') is most biggest number PS float('inf') 是最大的数
This works without lists and for number in any order.这适用于没有列表和任何顺序的数字。
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)
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)
Here is the algorithm of my approach:这是我的方法的算法:
Smallest=First_Input
and second_smallest = Second_Input
.Smallest=First_Input
和second_smallest = Second_Input
。 These lines do that: if counter == 0:
smallest = num
if counter == 1:
second = num
second_smallest
number, then swap them.second_smallest
数字,则交换它们。 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
smallest
and second_smallest
number.smallest
和second_smallest
数字。 If second_smallest < smallest
, then swap them.second_smallest < smallest
,则交换它们。 These lines do that: if second < smallest:
smallest, second = second, smallest
[smallest, smallest, x, y, z]
.[smallest, smallest, x, y, z]
将有一个边缘情况。 That is, when smallest number is repeated.smallest
and second
variables store the first smallest and second smallest numbers respectively.smallest
和second
变量分别存储了第一个最小和第二个最小的数字。 But when the smallest number is repeated, they both store the same value(ie the smallest
value).smallest
)。 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:second_smallest
值将无法替换second
中的值(因为在这种情况下second
存储了最小的)要解决这个问题,我们有这行代码: if second == smallest:
second = num
We can solve the problem by using another one temp variable.我们可以通过使用另一个临时变量来解决这个问题。
Let's assume to input one variable - v.让我们假设输入一个变量 - v。
First , compare v with the smallest number.首先,将 v 与最小的数进行比较。
If v is smaller than the smallest number, the smallest number will be v and the second small number will be a previous smallest number.如果 v 小于最小数字,则最小数字将是 v,第二个小数字将是先前的最小数字。
Second , if v is equal or bigger than the smallest number, compare with the second small number and handle it.其次,如果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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.