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
 
Answer
虽然很水,但是我好像水了很久...
题意是把数字各位相加,得到的和继续相加,直到和只有一位。
 
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
#define msd memset(dp,0,sizeof(dp))
using namespace std;
//#define LOCAL
string s;
int t=;
void solve()
{
for(string::iterator it=s.begin(); it!=s.end(); it++)
t+=*it-'';
s.clear();
while(!(t%==&&t/==))
{
s.push_back((t%)+'');
t/=;
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
while(cin>>s&&*(s.begin())!='')
{
solve();
while(s.size()!=)
solve();
printf("%c\n",s[]);
}
return ;
}

HDU 1013 Digital Roots(字符串)的更多相关文章

  1. HDU 1013 Digital Roots(to_string的具体运用)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...

  2. HDU 1013 Digital Roots【字符串,水】

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. HDU 1013 Digital Roots(字符串,大数,九余数定理)

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. HDU 1013.Digital Roots【模拟或数论】【8月16】

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

  5. HDU 1013 Digital Roots 题解

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  6. hdu 1013 Digital Roots

    #include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...

  7. HDU OJ Digital Roots 题目1013

     /*Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU - 1310 - Digital Roots

    先上题目: Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. 杭电 1013 Digital Roots

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013 反思:思路很简单,但是注意各位数加起来等于10的情况以及输入0的时候结束程序该怎么去表达 #in ...

随机推荐

  1. textarea定位光标

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. (翻译) Android Accounts Api使用指南

    本文翻译自Udinic的文章Write your own Android Authenticator,可能需要FQ才能阅读.这是译者目前能找到的介绍如何使用Android的Accounts Api最好 ...

  3. win7 安装 sql2000

    win7 安装:http://wenku.baidu.com/link?url=xNcfrMaMzX0KgBcjpMaySRaKITM2Op73ZI8sOX49zgl-GWPGB3vqye9gZA_c ...

  4. MongoDb注意事项

    1.保证足够大的内存. 2.Mongodb跟mysql一样,对于常用的查询条件,该建索引的建索引.不要认为内存够用,数据就会在内存,有时,内存够用,数据也不一定在内存. 3.使用副本集,一个主库,进行 ...

  5. 12157 - Tariff Plan

      Ampang Communications & Mobile (ACM) provides telecom services for various types of users. Sin ...

  6. classnull100 - The 3n + 1 problem

    新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正  The 3n + 1 problem  Background Problems in Computer Science are o ...

  7. Volt 模块引擎与phalcon框架组合使用指南

    ---- 近期工作中web页面使用由C语言编写的VOLT模板引擎,相比之前由js动态加载页面速度更快,更利于百度数据的抓取,现根据文档整理一下使用思路 (Volt是一个超快速和设计者友好的模板语言,C ...

  8. 首页导航点击A连接跳转并添加上背景色,

    <style> ul li { float: left; height: 50px; width: 120px; color: white; text-align: center; mar ...

  9. php bin/console doctrine:migrations:migrate

    $ php bin/console doctrine:migrations:migrate                    XinXiBundleMigrationsWARNING! You a ...

  10. grunt--自常用配置文件--js/样式压缩打包,sass工具整合使用

    // Project configuration. module.exports = function(grunt) { // 使用严格模式 'use strict'; // 这里定义我们需要的任务 ...