Python - 查找所有小于a且总和大于b的连续奇数正整数对
给定两个正整数a和b ,任务是在Python编写一个程序,以找到小于第一个数字a并且它们的总和应该大于第二个数字b 的所有连续奇数对。
例子:
Input:
a = 60
b = 100
Output:
Pairs of consecutive number are:
51 , 53
53 , 55
55 , 57
57 , 59
Input:
a = 20
b = 200
Output:
None
方法:
给出两个数字,然后检查它们是否为正整数,并检查第一个数字是否大于第二个数字的一半。然后我们将检查奇数正整数并分配给变量a。在while语句中,找到并打印成对的奇数连续整数。
示例 1:
Python3
# input first and second number
a = 60
b = 100
print('a =', a)
print('b =', b)
# check the first number should be greater
# than the half of second number and both number
# should be positive integer
if(a > 0 and b > 0 and a > b/2):
# to ensure value in firstNum variable
# must be odd positive integer
if(a % 2 == 0):
a -= 1
else:
a -= 2
b //= 2
print("Pairs of consecutive number are:")
# find the pairs of odd
# consecutive positive integer
while(b <= a):
if(b % 2 != 0):
x = b
if(x + 2 <= a):
print(x, ',', x+2)
b += 1
else:
print("None")
Python3
# input first and second number
a = 20
b = 200
print('a =', a)
print('b =', b)
# check the first number should be greater
# than the half of second number and both number
# should be positive integer
if(a > 0 and b > 0 and a > b/2):
# to ensure value in firstNum variable
# must be odd positive integer
if(a % 2 == 0):
a -= 1
else:
a -= 2
b //= 2
print("Pairs of consecutive number are:")
# find the pairs of odd
# consecutive positive integer
while(b <= a):
if(b % 2 != 0):
x = b
if(x + 2 <= a):
print(x, ',', x+2)
b += 1
else:
print("None")
输出:
a = 60
b = 100
Pairs of consecutive number are:
51 , 53
53 , 55
55 , 57
57 , 59
示例 2:
蟒蛇3
# input first and second number
a = 20
b = 200
print('a =', a)
print('b =', b)
# check the first number should be greater
# than the half of second number and both number
# should be positive integer
if(a > 0 and b > 0 and a > b/2):
# to ensure value in firstNum variable
# must be odd positive integer
if(a % 2 == 0):
a -= 1
else:
a -= 2
b //= 2
print("Pairs of consecutive number are:")
# find the pairs of odd
# consecutive positive integer
while(b <= a):
if(b % 2 != 0):
x = b
if(x + 2 <= a):
print(x, ',', x+2)
b += 1
else:
print("None")
输出:
a = 20
b = 200
None