我的程序:
#include<stdio.h> #include<math.h> int main(){ int n,a,b,s=0,t; scanf("%d",&n); a = (int)log10(n) + 1; b = a; while(b>1){ b--; t = pow(10,b); s += n/t; n = n%t; } s += n; printf("%d %d",a,s); }
网友的程序,更简洁:
#include <stdio.h> int main(){ int n, count, sum; sum = 0; count = 0; scanf("%d", &n); while(n!=0){ count++; sum += n%10; n /= 10; } printf("%d %d ", count, sum); return 0; }