zoukankan      html  css  js  c++  java
  • HDU 1013 Digital Roots

    Digital Roots

    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
     
    题目大意:求各个位的数字相加,重复这个过程直到结果只有一位数
     
    代码如下:
     1 # include <iostream>
     2 # include<cstdio>
     3 # include<cstring>
     4 # define LL long long
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     char s[10005];
    10     int n,temp,len,i;
    11     while(scanf("%s",s))
    12     {
    13         if(strcmp(s,"0")==0)
    14         break;
    15         len =strlen(s);
    16         n=0;
    17         for(i=0;i<len;i++)
    18         {
    19             n += s[i]-'0';
    20         }
    21         while(n>=10)
    22         {
    23             temp = n;
    24             n = 0;
    25             while(temp)
    26             {
    27                 n += temp%10;
    28                 temp /= 10;
    29             }
    30         }
    31         printf("%d
    ",n);
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    CentOS安装Nginx Pre-Built
    CMake设置编译参数
    SQLServer脚本编写
    使用QNetworkAccessManager实现Qt的FTP下载服务
    使用CMD命令设置IP
    IIS6(Win2003) 使用.net 4.0 后,默认文档失效解决方案。
    windows7打印时,显示脱机,提示“服务器打印后台处理程序服务没有运行”。
    阻止浏览器自动填表
    Java经典编程题50道之四
    Java经典编程题50道之三
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3621508.html
Copyright © 2011-2022 走看看