zoukankan      html  css  js  c++  java
  • hdu--1013--Digital Roots(字符串)

    Digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 77503    Accepted Submission(s): 24224


    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 /*
     2     Name: hdu--1013--Digital Roots
     3     Copyright: ©2017 日天大帝
     4     Author: 日天大帝
     5     Date: 22/04/17 10:34
     6     Description: 这个题,就是特别坑 
     7                 如果你一开始把所有的值设置为int型,恭喜你,你会得到一个WA
     8                 接着你大概会改成unsigned型,恭喜你,你会得到一个超时
     9                 然后你终于恍然大悟,用字符串!!
    10 */
    11 #include<iostream>
    12 #include<string> 
    13 using namespace std;
    14 int main(){
    15     ios::sync_with_stdio(false);
    16     
    17     string str;
    18     while(cin>>str,str!="0") {
    19         int sum = 0;
    20         for(char ch:str)sum += ch-48;
    21         while(sum>=10){
    22             int temp = sum;
    23             sum = 0;
    24             while(temp){
    25                 sum += temp%10;
    26                 temp /= 10;
    27             }
    28         }
    29         cout<<sum<<endl;
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    Django之cookie与session
    Django之在Python中调用Django环境
    Django之Django终端打印SQL语句
    Django之事务
    Django之ORM操作(聚合 分组、F Q)
    Linux常用服务安装部署
    Linux服务基础命令
    程序员的vim
    Linux的xshell命令
    Linux操作服务器的初识
  • 原文地址:https://www.cnblogs.com/langyao/p/6747127.html
Copyright © 2011-2022 走看看