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

  • 相关阅读:
    不要为自己找借口,你可以做到的--职场实用做人法则
    sql server 利用发布订阅方式实现数据库同步问题
    关于免费空间的寻找
    数据自定义格式化
    C++字符串string类常用操作详解(一)【初始化、遍历、连接】
    C++ 命名空间
    gcc/g++ 如何支持c11 / c++11标准编译
    正确的C++/C堆栈
    linux下清空c++ cin无效流的方式
    32位64位下各种数据类型大小的对比
  • 原文地址:https://www.cnblogs.com/clljs/p/7423722.html
Copyright © 2011-2022 走看看