数根
题目:输入一个数,然后将这个数的每一位加起来,如果得到的数不是10以内的数,就继续进行,知道是10以内的数之后直接输出
方法:由于题目中的这个数的位数为1-10001,所以明显不能使用数字,本题中我使用的是C++STL中的string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string n;
while (cin >> n)
{
int tmp = 0;
char tmp1 = 0;
while (n.size() > 1)
{
for (int i = 0; i < n.size(); i++)
{
tmp += (n[i] - '0');
}
n.clear();
while (tmp)
{
tmp1 = (tmp % 10) + '0';
n.push_back(tmp1);
tmp = tmp / 10;
}
}
cout << n << endl;
}
return 0;
}