D. calc the sum

Problem 234 from old oj

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int fun(int n);//循环往复,要用递归
int main()
{
	int t, i, sum = 0;
	char st[1000005];//题目所给数据超级大,所以不应使用long long,而应考虑字符串
	scanf("%d", &t);
	getchar();
	while (t--)
	{
		gets(st);
		for (i = 0; st[i]; i++)
			sum += st[i] - '0';
		printf("%d\n", fun(sum));
		sum = 0;
	}
	return 0;
}
int fun(int n)
{
	int sum = 0;
	if (n / 10 == 0)
		return n;
	else
	{
		
		while (n != 0)
		{
			sum += n % 10;
			n /= 10;
		}
		fun(sum);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43911865/article/details/85267790