【水】1021: Tom數-----ZZUOJ
阿新 • • 發佈:2018-12-05
1021: Tom數
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 31 Solved: 10
[ Submit][ Status][ Web Board]
Description
正整數的各位數字之和被Tom稱為Tom數。求輸入數(<2^32)的Tom數!
Input
每行一個整數(<2^32).
Output
每行一個輸出,對應該數的各位數之和.
Sample Input
12345
56123
82
Sample Output
15
17
10
HINT
Source
【思路】
通過對10取模得到末尾數字,然後累加求和就可以得到所謂Tom數;
請注意2^32的取值範圍,int 和long int都不夠的。
C++在while迴圈中鑲嵌輸入流即可讀取至檔案尾。
【程式碼】C++
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
long long n;
while(cin>>n)
{
int sum=0;
cin>>n;
while(n!=0)
{
sum+=n%10;
n/=10;
}
cout<<sum<<endl;
}
}