• HDOJ 1163 Edd's digital roots (数论)


        

    Eddy's digital Roots
    
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3561    Accepted Submission(s): 2013
    
    
    Problem 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.
    
    The Eddy's easy problem is that : give you the n,want you to find the n^n's digital Roots.
     
    
    Input
    The input file will contain a list of positive integers n, one per line. The end of the input will be indicated by an integer value of zero. Notice:For each integer in the input n(n<10000).
     
    
    Output
    Output n^n's digital root on a separate line of the output.
     
    
    Sample Input
    2
    4
    0
     
    
    Sample Output
    4
    4
     
    
    Author
    eddy
     
    
    Recommend
    JGShining
     

    刚看到这道题目的时候以为是原来做个的一道题目,

    后来发现输出结果是要n^n 的 digital roots,n是一个1到10000的整数,这样看来是无法存储的。

    我仔细考虑想找出一个规律,后来确实是发现了。

    比如5^5 的 digital root : 5^5 = 3125 3+1+2+5=11  ; digital为 1+1 =2;

                   也可以这样算5*5=25;digital为7;  7*5 = 35 digital为 8, 8*5=40 , digital为4; 4*5=20; digital为2;

    这样一来就可以有效地解决数没法存的问题了。

    敲完代码一遍AC,挺不错的。

    
    
    首先,推导一个定理
    
    将一个二位数表示成ab,对另一个表示为cd,两者可以相等,也可以不等,a、c表示十位,b、d表示个位
    
    那么
    
    ab*cd=(a*10+b)*(c*10+d)=a*c*100+a*d*10+b*c*10+b*d
    
    则 有
    
    ab*cd=(a+b)*(c+d)
    
    同理可证多位数。
    
    从上面的推导,我们可以知道:
    
    N个数相乘后的根=N个数的根相乘后该数的根
    
    然后,再谈谈我的一个发现
    
    当判断一个数的根的时候,只需对9取余即可,即:
    
    当number%9==0时,根=9;
    
    当number%9!=0时,根=number%9
    
    
    


    1
    #include<stdio.h> 2 3 4 int change(int n) 5 { 6 int t=0; 7 while(n) 8 { 9 t+=n%10; 10 n/=10; 11 } 12 return t; 13 } 14 int main() 15 { 16 int N, t, i; 17 while(scanf("%d",&N)!=EOF) 18 { 19 if(N==0) break; 20 t = N; 21 for(i=1;i<N;i++) 22 { 23 t*=N; 24 t = change(t); 25 while(t>=10) 26 { 27 t = change(t); 28 } 29 } 30 printf("%d ",t); 31 } 32 }
     1 //后来又看到别人的一种做法,叫做九余数定理
     2 //学习了
     3 #include<stdio.h>
     4 #include<string.h>
     5 #include<algorithm>
     6 using namespace std;
     7 int main()
     8 {
     9     int n,i;
    10     int m;
    11     while(scanf("%d",&n)!=EOF&&n)
    12     {
    13         m=n;
    14         for(i=2;i<=n;i++)
    15         {
    16             m=(m*n)%9;
    17         }
    18         if(m)
    19         printf("%d
    ",m);
    20         else
    21         printf("9
    ");
    22     }
    23     return 0;
    24 }
    25 //一个数对九取余,得到的数称之为九余数;
    26 //一个数的九余数等于它的各个数位上的数之和的九余数!
  • 相关阅读:
    Android实现资料收藏
    GPS(Global Positioning System)全球定位系统
    Android开发环境搭建全程演示(jdk+eclipse+android sdk)
    ADT在线安装
    简洁判断一个byte中有多少位为1的bit?
    tomcat部署java项目
    centos7把编译安装的服务通过systemctl管理
    ansible自动化部署
    redi通过哨兵sentinel实现主从切换
    redis实现集群加主从复制
  • 原文地址:https://www.cnblogs.com/Lee-geeker/p/3257382.html
Copyright © 2020-2023  润新知