您的位置:首页 > 其它

HDOJ  1013   Digital Roots

2015-12-18 18:07 435 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1013

#include <stdio.h>

#include <math.h>

#include <string.h>

int root(int a)

{

int t=0;


while(a>9)

{


t+=a;


a=(a-a)/10;

}

if(t+a>9)


t=root(t+a);

else


t+=a;

return t;

}

int main()

{

char c[1001];

int n;

while(scanf("%s",c)!=EOF
&& (c[0]!='0'||c[1]!='\0'))

{

int
a=0,i;

for(i=0;
c[i]!='\0'; i++)


a=a+c[i]-'0';


printf("%d\n",root(a));


memset(c,0,sizeof(c));

}

return 0;

}

**************************************************

感觉下面这种代码也挺好的

#include <iostream>

using namespace std;

int main()

{

int
a,sum;

string
b;


while(cin>>b,b!="0")

{


a=0;


for(int i=0;i<b.length();i++)


a+=b[i]-'0';


while(a>=10)


{


sum=0;


while(a>=10)


{


sum+=a;


a/=10;


}


sum+=a;


a=sum;


}


cout<<a<<endl;

}

return
0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: