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

  • 相关阅读:
    中文字体
    URL锚点HTML定位技术机制
    关于JS异步加载方案
    select标签用法
    javascript refresh page 几种页面刷新的方法
    用JavaScript刷新框架子页面的七种方法
    robots.txt用法
    netstat 基本用法
    linux下ps命令
    socket编程和并发服务器
  • 原文地址:https://www.cnblogs.com/clljs/p/7423722.html
Copyright © 2011-2022 走看看