[英]Fastest way to calculate a digit-sum for a large number (as a decimal string)
[英]How to solve the sum of three digit numbers,with how to calculate the second number
once = number/100;
thenth = (number%100)/10;
hundred = number%10;
sum = once + thenth + hundred;
printf("Sum of entered digits is %d\n", sum);
如何计算第二个数字,例如(345%100)/10=34.5如何计算第二个数字,任何人都可以解释该程序的逻辑
(number % 100)
对于非负number
提取最后两位数字,并除以由10
将有效地丢弃由于整数除法单元位。 因此,这是计算thenth
。
你可以砍掉的至少显著数字更优雅的解决了这个number
,当您去:
hundred = number % 10;
number /= 10;
tenth = number % 10;
number /= 10;
once = number;
希望能够将其滚动成一个循环以解决更一般的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.