CodeForces 102B Sum of Digits

http://codeforces.com/problemset/problem/102/B


不停的把一个数位数相加,直到是一个个位数


#include<bits/stdc++.h>
using namespace std;
struct node{
	char aa;
	int da;
};char a[111111];
bool cp(node x,node y)
{
	return x.da>y.da;
}
int main(){
	int k;
	while(cin>>a)
	{
	    int sum=0;
	    if(strlen(a)==1)
	    {
	    	cout<<"0"<<endl;
	    	continue;
		}
	    for(int i=0;i<strlen(a);i++)
	    {
	    	sum+=a[i]-'0';
		}
		int ans=sum;
		int js=0;
		while(ans/10>0)
		{
			int t=0;
			while(sum)
			{
				t+=sum%10;
				sum/=10;
			}
			sum=ans=t;
			js++;
		}
		cout<<js+1<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/threeh20/article/details/80191817
今日推荐