zoukankan      html  css  js  c++  java
  • hdu_1013_Digital Roots_201310121652

    Digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 40786    Accepted Submission(s): 12584


    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.
     
    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
     
    第一次做的:(没考虑大数据的问题)
     
     1 #include <stdio.h>
     2 
     3 int f(int t)
     4 {
     5     int s=0;
     6     while(t>0)
     7     {
     8         s+=t%10;
     9         t/=10;
    10     } 
    11     return s;
    12 }
    13 
    14 int main()
    15 {
    16     int n;
    17     while(scanf("%d",&n),n)
    18     {
    19         int i,j,t,s;
    20         t=n;
    21         if(t<10)
    22         printf("%d
    ",t);
    23         else if(t>=10)
    24         {
    25             s=f(t);
    26             while(s>=10)
    27             {
    28                 s=f(s);
    29             }
    30             printf("%d
    ",s);
    31         }        
    32     }
    33     return 0;
    34 }
    35 //wa

    第二次做的:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 char s[10001];
     5 
     6 int f(int t)
     7 {
     8     int s=0;
     9     while(t>0)
    10     {
    11         s+=t%10;
    12         t/=10;
    13     } 
    14     return s;
    15 }
    16 
    17 int main()
    18 {
    19     while(scanf("%s",s)&&s[0]!='0')
    20     {
    21         int i,t,sum=0;
    22         for(i=0;i<strlen(s);i++)
    23         sum+=s[i]-'0';
    24         if(sum<10)
    25         printf("%d
    ",sum);
    26         else if(sum>=10)
    27         {
    28             t=f(sum);
    29             while(t>=10)
    30             {
    31                 t=f(t);
    32             }
    33             printf("%d
    ",t);
    34         }        
    35     }
    36     return 0;
    37 }
    38 //ac

    链接(大神做法):http://www.cppblog.com/ArcTan/articles/167330.html

    hdu1013(模拟&数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=1013


    这个题模拟也可以AC,刚开始我也是模拟AC的。不过看了百度看了大牛的博客,感谢大牛,知道了还有数论这回事。

    n=0 1 2 3 4 5 6 7 8 9 10 11 12 13 ......... 100 101 102 103 ....
    roots=0 1 2 3 4 5 6 7 8 9 1 2 3 4 .......1 2 3 4....
    原来是以1.....9为循环节的。想想也是,每次增加1,那么层层迭代下来,最终当ans<10的时候也是每次增加了1。如此,可以归纳出
    roots=(n-1)%9+1

    注意输入的数字很大需要字符串读入,求和得n:

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

    int main()
    {
        
    int i,n,tmp;
        
    char a[1003];
        
    while (scanf("%s",&a)&&a[0]!='0')
        {
            n
    =0;
            
    for (i=0;i<strlen(a); i++)
            {
                n
    +=a[i]-48;
            }
            printf(
    "%d ",(n-1)%9+1);
        }
        
    return 0;
    }
  • 相关阅读:
    《Linux设备驱动开发详解(第3版)》(即《Linux设备驱动开发详解:基于最新的Linux 4.0内核》)--宋宝华
    WebApp 安全风险与防护课堂开课了!
    2019 Vue开发指南:你都需要学点啥?
    每个程序员都该学习的5种开发语言
    每个Web开发者都该了解的12条命令行
    Webpack 4教程
    都 9012了,该选择 Angular、React,还是Vue?
    在 Angular 8 中,我们可以期待些什么
    “宇宙最强” IDE,Visual Studio 2019 正式发布
    Webpack 4教程
  • 原文地址:https://www.cnblogs.com/xl1027515989/p/3365625.html
Copyright © 2011-2022 走看看