UVa10922:2 the 9s

题目: 1.判断每个数是不是9的倍数,2.判断每个位数的总和是不是9的倍数,  并统计次数, 然后判断其总数的每个位数总和是否为9的倍数 并计数, 以此类推。(999 -> 27 -> 9   --->  degree = 2) 


我們知道要怎麼確定一個整數是不是 9 的倍數-如果它每位數的總和是9的倍數,那它就是9的倍數。這種檢驗的方法其實是一種遞迴的方法,而且我們把這種方法遞迴的深度稱作 N 的 9-degree 。

你的工作就是,給你一個正整數N,判斷他是不是9的倍數,而且如果他是9的倍數你還需要判斷它的 9-degree。

Input 

輸入含有多組測試資料。每組測試資料一列包含一個正數 N。

當 N=0 時代表輸入結束;輸入的數最大可以到1000位數。

Output 

對於每一組測試資料,請輸出它是否是 9 的倍數及它的 9-degree。輸出格式請參考Sample Output。

Sample Input 

999999999999999999999
9
9999999999999999999999999999998
837
0

Sample Output 

999999999999999999999 is a multiple of 9 and has 9-degree 3.
9 is a multiple of 9 and has 9-degree 1.
9999999999999999999999999999998 is not a multiple of 9.
837 is a multiple of 9 and has 9-degree 2.

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

int main()
{	
	while(1)
	{
		char arr[1001]={0};
		int i, sum=0, count=0, degree=0;
	
		gets(arr);
		if(arr[0]-'0'==0) break;
	
		for(i=0; i<strlen(arr); i++)
			sum+=arr[i]-'0';
		
		while(sum%9==0)
		{
			if(sum==9)
			{
				degree++;
				break;
			}
			
			count=0;
			while(sum!=0)
			{
				count+=sum%10;
				sum/=10;
			}
			sum=count;
			degree++;
		}
		
		printf("%s ",arr);
		if(sum%9==0)  printf("is a multiple of 9 and has 9-degree %d.\n",degree);
		else printf("is not a multiple of 9.\n");
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值