- zhuzixin 的博客
拆位和(样例)代码(可复制)
- 2024-12-14 9:08:40 @
代码:
#include<iostream>
#include<cmath>
using namespace std;
int chaiweihe(long long a)//拆位和(缩写)函数
{
long long s = 0;
while(a > 0)//重点记忆!(函数重中之重)
{
s += a % 10;
a /= 10;
}
return s;
}
int main()
{
cout << "请输入一个数字(不能大于2147483647): \n";
long long n;
cin >> n;//测试样例
if(n > 2147483647)
{
cout << "输入错误! \n";
return 0;
}
cout << n << "的各位数字相加为" << chaiweihe(n) << "\n";
return 0;
}