HDOJ-1013 Digital Roots

http://acm.hdu.edu.cn/showproblem.php?pid=1013

1.给出一个整数,求每一位上的数字之和

2.若求出的和大于1位,则对该和继续执行第1步,直至和的位数为1

注意:该整数有可能远远大于int或__int64的取值范围,所以用字符数组处理

# include <stdio.h>
# include <string.h>

int main()
{
	char num[100000], i;//输入整数长度可能远远超过整数范围

	while(scanf("%s",num) && strcmp(num, "0"))
	{
		int temp = 0;
		for(int i = 0; num[i] != '\0'; i++)//第一次对输入的整数求位和
			temp += num[i] - '0';

		while(temp >= 10)//若位和的位数大于1位 重复求位和操作
		{
			int t = temp;
			temp = 0;
			while(t > 0)
			{
				temp += t % 10;
				t = t / 10;
			}
		}
		printf("%d\n",temp);
	}

	return 0;
}

  

posted @ 2015-04-26 10:21  懒人の猫  阅读(118)  评论(0编辑  收藏  举报