• HDU1013 Digital Roots


    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
    题意:给一串数字,使每个位上的数字相加,如果结果大于等于10 ,循环这个过程,知道结果小于十,输出。
          实际上是个大数问题,给出的数字可能已超过了LONG LONG 范围,可以用字符串处理。
     
     1 #include<cstring>
     2 #include<cstdio>
     3 #include<iostream>
     4 #include<cstdlib>
     5 
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     char str[20000];
    11     while(cin>>str&&str[0]!='0')
    12     {
    13         int sum;
    14         do{
    15                 sum=0;
    16             for(int i=0;i<strlen(str);i++)
    17                 sum+=(str[i]-'0');
    18             itoa(sum,str,10);  //将数字转换为字符串 itoa(数字,字符串首地址,进制);
    19         }while(sum>=10);
    20         cout<<sum<<endl;
    21     }
    22 }
  • 相关阅读:
    从json中获取自己想要的属性
    对称加密解密
    springboot 读取resource目录下的文件
    安装nginx 配置了ssl 启动报错
    复利计算--4.0 单元测试之JAVA版-软件工程
    《构建之法》前三章读后感
    单利 复利计算器程序1.0 2.0 3.0 [ 合 ] 之 WEB
    操作系统 实验一 命令解释程序的编写
    单利 复利计算器程序1.0 2.0 3.0 [ 合 ] 之 C语言
    统计实验数据 总结实验结果
  • 原文地址:https://www.cnblogs.com/wsaaaaa/p/4287459.html
Copyright © 2020-2023  润新知