zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    A journey on the Android Main Thread
    android的消息处理机制(图+源码分析)——Looper,Handler,Message
    用fragment创建一个选项卡
    fragment的生命周期
    动态创建fragment
    自定义自动换行布局
    Android开源框架Universal-Image-Loader完全解析(三)
    第七章 摄影技术
    第六章 镜头的成像像质评价
    第五章 镜头像差的成因,校正及规避方法
  • 原文地址:https://www.cnblogs.com/clljs/p/7423722.html
Copyright © 2011-2022 走看看