• 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 }
  • 相关阅读:
    [leetcode-604-Design Compressed String Iterator]
    [leetcode-617-Merge Two Binary Trees]
    OpenCV学习1-----打开摄像头并在画面上添加水印
    cvCvtColor与cvtColor区别
    [leetcode-547-Friend Circles]
    [leetcode-260-Single Number III]
    复位电路
    单片机特殊功能寄存器
    单片机的定时器与计数器
    单片机定时/计数工作方式
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3621508.html
Copyright © 2020-2023  润新知