Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴一个简单的递归代码,简短

Posted by 1004111222 at 2012-08-10 14:03:38 on Problem 1519
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator