zoukankan      html  css  js  c++  java
  • hdu 1013 Digital Roots

    Digital Roots

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


    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
     
    Source
     
    Recommend
    We have carefully selected several similar problems for you:  1048 1032 1170 1015 1196 
     
    一道水题,不过和数学有关,需要使用到一个定理,一个数 模9等于各位数字和模9,例如 33%9=6%9,输入的数字可能很大,需要使用字符串输入。
     
    题意:输入一个数字,将每一位上的数字加起来,直到和为一位数时输出,否则继续将每位数字之和相加。
     
    附上代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 int main()
     5 {
     6     char a[10100];
     7     int i,n,t,s,k;
     8     while(scanf("%s",a))
     9     {
    10         s=0;
    11         t=strlen(a);
    12         if(a[0]=='0'&&t==1)
    13             break;
    14         for(i=0; i<t; i++)
    15             s+=a[i]-'0';  //输入的数字有可能很大,不能直接对9取模,所有先将每一位数字之和加一次
    16         k=s%9;
    17         if(k==0)       //如果结果等于0,这个数其实等于9
    18             k+=9;
    19         printf("%d
    ",k);
    20     }
    21     return 0;
    22 }
  • 相关阅读:
    闭包的坑
    python中@property和property函数使用
    Python3运算符
    内置函数——eval、exec、compile
    内置函数和匿名函数
    迭代器和生成器
    PHP内置的字符串处理函数
    PHP中的抽象类和接口
    PHP面向对象中常用的关键字和魔术方法
    PHP面向对象的程序设计一些简单的概念
  • 原文地址:https://www.cnblogs.com/pshw/p/4763108.html
Copyright © 2011-2022 走看看