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;
    }

  • 相关阅读:
    网站推荐:11个相似图片搜索网站(以图找图)
    逻辑回归
    关于估计、偏差以及方差
    算法面试问题集锦
    sql语句执行顺序
    五大算法之动态规划
    五大算法之回溯算法
    操作系统问题总结之处理机调度
    操作系统问题总结之进程管理
    Element-UI 去掉表格边框(表格Hover事件移除)
  • 原文地址:https://www.cnblogs.com/clljs/p/7423722.html
Copyright © 2011-2022 走看看