题解 | 数位之和
数位之和
https://www.nowcoder.com/practice/5e85cc26475449648e668aa98e92d05b
#include <iostream>
#include <sys/types.h>
using namespace std;
int sum(int n)
{
int a;
int b = 0;
if ( n == 0)
{
return 0;
}
while(n)
{
a = n%10;
n = n/10;
b = b + a;
}
return b;
}
int main() {
int n ;
cin >> n;
cout << sum(n) << endl;
}
// 64 位输出请用 printf("%lld")
查看9道真题和解析