HDU 1013Digital Roots

Description

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit. 

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39. 
 

Input

The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero. 
 

Output

For each integer in the input, output its digital root on a separate line of the output. 
 

Sample Input

    
    
24 39 0
 

Sample Output

    
    
6 3
找的一点规律: 事实上,用任何一种方法得到一个大数的各位数字相加得到一个和,这个和又是一个新的数,把这个新的数的各位数字相加又得到一个和,如此,重复刚才的过程,只到最后的数字之和是一位数为上.那么这个数就是原数除以 9 的余数,我们把这个余数称之为原数的 " 数字根 " 根据同余原理,我们知道,在求一个数的数字根时,可以把原数的数字 9 舍去,相加得 9 后,也可以舍去.例如,求 549721 的数字根时,其中有 9 ,而且 5+4 7+2 都是 9 ,尽可以舍去,最后只剩下 1 ,这就是原数的数字根. 事实上,一个数,将它的各个数字重排,获得了一个新数.但原数和新数的数字根相同,也就是被 9 除有相同的余数.把这两个数相减后,又得到一个数.由同余原理知道,这个数就会是 9 的倍数,它的数字根是 0 9 .再经过刚才辗转的过程,再得到一个两位数.事实上,被 9 整除的两位数的数字之和一定是 9。
SO:
<span style="font-size:18px;">#include <iostream>
#define LEN 5000
using namespace std;
  int main()
  {
      int sum;
      char input[LEN]={""};
      while(1)
     {
         sum=0;
         cin>>input;
        for(int i=0;input[i]!='\0';i++)
         {
            sum+=(input[i]-'0');
             input[i]='\0';
         }
         if(sum==0)break;
         if(sum%9==0)cout<<9<<endl;
         else cout<<sum%9<<endl;
     }
     return 0;
 }</span>

数可能很大
#include<stdio.h>
int main()
{    
    int n,mul,num,i;   
    while (scanf("%d",&n)&&n)    
    {
    	num=n;
    	n%=9;
    	mul=1;
    	for (i=1;i<=num;i++)
    	    mul=(mul*n)%9;
    	if (mul==0)
    	    mul=9;
    	printf("%d\n",mul);
    }
    return 0;
}


 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值