题解 | #数位之和#
数位之和
https://www.nowcoder.com/practice/5e85cc26475449648e668aa98e92d05b
#include <stdio.h> int main() { //求一个整数的所有数位之和 int n = 0; scanf("%d",&n); int sum = 0; while (n>0) { sum = sum +n%10; //运行超时的原因:循环始终运行 n = n/10; //去掉了后一位 } printf("%d",sum); return 0; }