如何在整数中添加数字
How to add numbers in an integer
我是 python 的完全 (GCSE) 初学者。
我正在尝试将数字添加到一个整数中。我的变量 FNT 每次都以不同的数字出现(取决于我之前输入的内容),然后我需要这些数字自行相加。例如。
FNT=19
我现在需要这个号码来做这件事-
1+9=10
1+0=1 这些数字需要不断相加,直到它们是一位数,但每次的数字都可能不同。非常感谢所有帮助,但是,正如我所说,我是一个完全的初学者,可能无法理解任何太复杂的东西,所以有人知道该怎么做吗?
要对一个数的所有数字求和,您可以尝试这样的一行代码:
sum(int(chr) for chr in str(number))
您可以重复应用直到结果小于 10:
res = number
while True:
res = sum(int(chr) for chr in str(res))
if res < 10:
break
res
现在存储您的结果。
有两种方法:数学方法和使用字符串在 python.
中可迭代这一事实的方法
数学方法使用模(%
)和积分除法(//
)将数字分解为数字:
number = int(input('What number do you want to start with? '))
while number > 9:
decompose_helper, number = number, 0
while decompose_helper: # != 0 is implied
number += decompose_helper % 10
decompose_helper = decompose_helper // 10
print('Result is', number)
您可以使用 divmod
函数改进此代码:
number = int(input('What number do you want to start with? '))
while number > 9:
decompose_helper, number = number, 0
while decompose_helper: # != 0 is implied
decompose_helper, remainder = divmod(decompose_helper, 10)
number += remainder
print('Result is', number)
可迭代字符串方式:
number = input('What number do you want to start with? ')
while len(number) > 1:
number = str(sum(int(digit) for digit in number))
print('Result is', number)
这些代码都不处理输入验证,所以如果您的用户输入的不是整数,代码就会崩溃。您可能需要处理它。
我推荐使用数学方法,因为它更快。时序(删除 input
和 print
)为:
>>> timeit.timeit('math_way("4321234123541234")', setup='from __main__ import math_way', number=10000)
0.06196844787336886
>>> timeit.timeit('str_way("4321234123541234")', setup='from __main__ import str_way', number=10000)
0.10316650220192969
这可能不是最优化的,但它应该做到:
def addThem(FNT):
while FNT >= 10:
FNT = sum([int(i) for i in list(str(FNT))])
return FNT
编辑:
如果 FNT = 19,列表(str(FNT))returns ['1','9']。
列表理解 [int(i) for i in list(str(FNT))] returns [1, 9](注意这些现在是整数)
有更好的方法可以做到这一点,但作为编程练习,这应该会让您有所了解。
while digits>10:
t=0
for c in str(digits):
t+=int(c)
digits=t
将输入的数字转换为字符串并遍历数字,重复直到答案是个位数。
这是在 java 中完成的,但它仍然与 python 中使用的逻辑相同。
我用了一个split
,把数字分开,然后我把每个分开的数字都转换成一个整数,然后把它们加起来。我现在使用 if statement
来检查总和是否大于或等于 10。如果是,那么我再次重复该过程。
这是程序
String num = "19"; //you can make use of any value here
String array1[] = num.split("");
int sum = 0;
for (int i = 1; i <= num.length(); i++) {
sum = sum + Integer.parseInt(array1[i]); //converting each element to an integer then sum it up.
}
if (sum >= 10) {
String newnum = sum + ""; //converting the sum to a string
String newarray[] = newnum.split("");
int newsum = 0;
for (int i = 1; i <= newnum.length(); i++) {
newsum = newsum + Integer.parseInt(newarray[i]);
}
System.out.println( newsum);
}else{
System.out.println(num);
}
输出
1
我是 python 的完全 (GCSE) 初学者。 我正在尝试将数字添加到一个整数中。我的变量 FNT 每次都以不同的数字出现(取决于我之前输入的内容),然后我需要这些数字自行相加。例如。 FNT=19 我现在需要这个号码来做这件事- 1+9=10 1+0=1 这些数字需要不断相加,直到它们是一位数,但每次的数字都可能不同。非常感谢所有帮助,但是,正如我所说,我是一个完全的初学者,可能无法理解任何太复杂的东西,所以有人知道该怎么做吗?
要对一个数的所有数字求和,您可以尝试这样的一行代码:
sum(int(chr) for chr in str(number))
您可以重复应用直到结果小于 10:
res = number
while True:
res = sum(int(chr) for chr in str(res))
if res < 10:
break
res
现在存储您的结果。
有两种方法:数学方法和使用字符串在 python.
中可迭代这一事实的方法数学方法使用模(
%
)和积分除法(//
)将数字分解为数字:number = int(input('What number do you want to start with? ')) while number > 9: decompose_helper, number = number, 0 while decompose_helper: # != 0 is implied number += decompose_helper % 10 decompose_helper = decompose_helper // 10 print('Result is', number)
您可以使用
divmod
函数改进此代码:number = int(input('What number do you want to start with? ')) while number > 9: decompose_helper, number = number, 0 while decompose_helper: # != 0 is implied decompose_helper, remainder = divmod(decompose_helper, 10) number += remainder print('Result is', number)
可迭代字符串方式:
number = input('What number do you want to start with? ') while len(number) > 1: number = str(sum(int(digit) for digit in number)) print('Result is', number)
这些代码都不处理输入验证,所以如果您的用户输入的不是整数,代码就会崩溃。您可能需要处理它。
我推荐使用数学方法,因为它更快。时序(删除 input
和 print
)为:
>>> timeit.timeit('math_way("4321234123541234")', setup='from __main__ import math_way', number=10000)
0.06196844787336886
>>> timeit.timeit('str_way("4321234123541234")', setup='from __main__ import str_way', number=10000)
0.10316650220192969
这可能不是最优化的,但它应该做到:
def addThem(FNT):
while FNT >= 10:
FNT = sum([int(i) for i in list(str(FNT))])
return FNT
编辑:
如果 FNT = 19,列表(str(FNT))returns ['1','9']。
列表理解 [int(i) for i in list(str(FNT))] returns [1, 9](注意这些现在是整数)
有更好的方法可以做到这一点,但作为编程练习,这应该会让您有所了解。
while digits>10:
t=0
for c in str(digits):
t+=int(c)
digits=t
将输入的数字转换为字符串并遍历数字,重复直到答案是个位数。
这是在 java 中完成的,但它仍然与 python 中使用的逻辑相同。
我用了一个split
,把数字分开,然后我把每个分开的数字都转换成一个整数,然后把它们加起来。我现在使用 if statement
来检查总和是否大于或等于 10。如果是,那么我再次重复该过程。
这是程序
String num = "19"; //you can make use of any value here
String array1[] = num.split("");
int sum = 0;
for (int i = 1; i <= num.length(); i++) {
sum = sum + Integer.parseInt(array1[i]); //converting each element to an integer then sum it up.
}
if (sum >= 10) {
String newnum = sum + ""; //converting the sum to a string
String newarray[] = newnum.split("");
int newsum = 0;
for (int i = 1; i <= newnum.length(); i++) {
newsum = newsum + Integer.parseInt(newarray[i]);
}
System.out.println( newsum);
}else{
System.out.println(num);
}
输出
1