• hd acm1013


    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
     
    Sample Way
     

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

    int main()
    {
      char num[2000];
      int sum,sum1,n,i;
      memset(num,0,sizeof(num));           //将字符型数组全部赋值为‘0’。
      gets(num);                                      //将用户输入的整数以字符型存入num。
      while(strcmp(num,"0")!=0)              
      {
      sum1=sum=0;
      n=strlen(num);
        for(i=0;i<n;i++)

        {
        sum1+=(num[i]-'0');                //注意char型跟int型通用,这个地方查ASCII可知原理
        }
        while(1)            //while循环求出数根。
        {
        sum+=sum1%10;                  
        sum1/=10;
        if(sum1==0)
         {
          if(sum<10)
          {
          printf("%d ",sum);
          break;
          }
          else
          {
          sum1=sum;
          sum=0;
          }
           }
        }
      memset(num,0,sizeof(num));
      gets(num);
      }
    return 0;
    }

  • 相关阅读:
    hdu 1028 Ignatius and the Princess III (n的划分)
    CodeForces
    poj 3254 Corn Fields (状压DP入门)
    HYSBZ 1040 骑士 (基环外向树DP)
    PAT 1071 Speech Patterns (25)
    PAT 1077 Kuchiguse (20)
    PAT 1043 Is It a Binary Search Tree (25)
    PAT 1053 Path of Equal Weight (30)
    c++ 常用标准库
    常见数学问题
  • 原文地址:https://www.cnblogs.com/clljs/p/7423722.html
Copyright © 2020-2023  润新知