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

Re:原来递归不行。。。。

Posted by scu_sadan at 2010-09-24 18:21:32 on Problem 1519
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:
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