码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ-1013 Digital Roots

时间:2015-04-26 10:48:07      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

  

HDOJ-1013 Digital Roots

标签:

原文地址:http://www.cnblogs.com/linjiaman/p/4457382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!