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 |
贴一个简单的递归代码,简短#include <iostream> #include <cstdio> #include <cstring> #define MAXN 10000 using namespace std; char s[MAXN]; int func(int n) { int sum = 0; while(n) { sum += (n)%10; n /= 10; } if(sum<10) return sum; return func(sum); } int main() { int i,j,n,m; while(scanf("%s",s) != EOF) { if(s[0] == '0') break; int sum = 0; int len = strlen(s); for(i=0; i<len; i++) sum += (s[i] - '0'); printf("%d\n",func(sum)); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator