Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:原来递归不行。。。。In Reply To:Re:原来递归不行。。。。 Posted by:scu_sadan at 2010-09-24 18:20:09 > 可以用递归实现的,不过必须在开始输入的时候输入字符串,然后转换为整数进行处理 附代码: #include<iostream> using namespace std; int digitroot(int n) { int m; if(n<10) return n; else { m=0; while(n!=0) { m+=n%10; n=n/10; } return digitroot(m); } } int main(void) { char str[2000]; while(scanf("%s",str)==1) { int i,n; if(strcmp(str,"0")==0) break; for(i=0,n=0;i<strlen(str);i++) n+=str[i]-'0'; printf("%d\n",digitroot(n)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator