题解 | #数位之和#
数位之和
http://www.nowcoder.com/practice/5e85cc26475449648e668aa98e92d05b
#include<stdio.h>
#include<math.h>
int main() {
int a, temp, count = 0, val = 0; //count计算位数,temp暂存值
scanf("%d", &a);
temp = a;
while (a > 0) {
a = a / 10;
count++;
}
for (int i = count ; i > 0; i--) {
val += temp % 10; //结果加个位值
temp /= 10; //消除个位值 例 123 -> 12
}
printf("%d", val);
return 0;
}
#include<math.h>
int main() {
int a, temp, count = 0, val = 0; //count计算位数,temp暂存值
scanf("%d", &a);
temp = a;
while (a > 0) {
a = a / 10;
count++;
}
for (int i = count ; i > 0; i--) {
val += temp % 10; //结果加个位值
temp /= 10; //消除个位值 例 123 -> 12
}
printf("%d", val);
return 0;
}