仅使用数字运算从数字中删除偶数位,没有列表或字符串
Remove even digits from a number with only numerical operations, no list or strings
对于这个任务,我的任务是:
- Input a number
- Remove even digits from the given number without changing the order of the digits
For example: 123407 will print 137
If a number starts with 246 then it prints 0.
不允许我使用字符串、列表、函数、包、递归...,只能使用数学运算。
到目前为止,这是我的代码:
POSITION = 1 # give the digit position of the number
OUTPUT = 0 # printing the output of the result
enterNum = int(input('Enter a Number')) # user inputs a number
while enterNum != 0:
digit = enterNum % 10 # last digit of the number if number is odd
enterNum = enterNum / 10 # shift decimal places and remove the digit position
if digit % 2 == 1:
OUTPUT += POSITION * digit # if digit is odd, add it to the position
else:
continue
print(OUTPUT)
我完全不知道该做什么。当数字是123407时,我只能打印7。我不知道如何将数字存储在数据类型中,最后将数字组合起来得到137。此外,我很难循环遍历数字.它只给了我 7.
这是我的方法:
num = 123407
res, power = 0, 1
while num:
last_digit = num % 10
if last_digit % 2 == 1:
res += last_digit * power
power *= 10
num = num // 10
print(res)
# 137
提示:
你通过迭代从右到左得到一个数字的数字
d = n % 10
n = n / 10
你从右到左构造一个数字:
n+= d * p
p*= 10
(最初是p = 1
。)
现在结合这两个过程来传输奇数位。
测试过了。从您的方法开始,只需进行一些修改(有关修改的详细信息,请参阅评论):
POSITION=0 # This starts at 0, this is because the power of 10 is 0 based (position 0 is the 1's place of the answer)
OUTPUT=0
enterNum= int(input('Enter a Number'))
while enterNum!=0:
digit=enterNum%10
enterNum=enterNum/10 #This will work for python2 I think, but in python3 you will need enterNum//10
if digit%2==1:
OUTPUT+=(digit * 10**POSITION) # The digit needs to go in the current place value, if it is the 3rd digit, it needs to be multiplied by 100 (or 10^2).
POSITION += 1 # Add 1 to the position since we found one (need to place the next OUTPUT digit at the next position)
else:
continue
print(OUTPUT)
对于这个任务,我的任务是:
- Input a number
- Remove even digits from the given number without changing the order of the digits
For example: 123407 will print 137
If a number starts with 246 then it prints 0.
不允许我使用字符串、列表、函数、包、递归...,只能使用数学运算。
到目前为止,这是我的代码:
POSITION = 1 # give the digit position of the number
OUTPUT = 0 # printing the output of the result
enterNum = int(input('Enter a Number')) # user inputs a number
while enterNum != 0:
digit = enterNum % 10 # last digit of the number if number is odd
enterNum = enterNum / 10 # shift decimal places and remove the digit position
if digit % 2 == 1:
OUTPUT += POSITION * digit # if digit is odd, add it to the position
else:
continue
print(OUTPUT)
我完全不知道该做什么。当数字是123407时,我只能打印7。我不知道如何将数字存储在数据类型中,最后将数字组合起来得到137。此外,我很难循环遍历数字.它只给了我 7.
这是我的方法:
num = 123407
res, power = 0, 1
while num:
last_digit = num % 10
if last_digit % 2 == 1:
res += last_digit * power
power *= 10
num = num // 10
print(res)
# 137
提示:
你通过迭代从右到左得到一个数字的数字
d = n % 10
n = n / 10
你从右到左构造一个数字:
n+= d * p
p*= 10
(最初是p = 1
。)
现在结合这两个过程来传输奇数位。
测试过了。从您的方法开始,只需进行一些修改(有关修改的详细信息,请参阅评论):
POSITION=0 # This starts at 0, this is because the power of 10 is 0 based (position 0 is the 1's place of the answer)
OUTPUT=0
enterNum= int(input('Enter a Number'))
while enterNum!=0:
digit=enterNum%10
enterNum=enterNum/10 #This will work for python2 I think, but in python3 you will need enterNum//10
if digit%2==1:
OUTPUT+=(digit * 10**POSITION) # The digit needs to go in the current place value, if it is the 3rd digit, it needs to be multiplied by 100 (or 10^2).
POSITION += 1 # Add 1 to the position since we found one (need to place the next OUTPUT digit at the next position)
else:
continue
print(OUTPUT)