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. 【NET】Winform用户控件的初步封装之列表页控件

    public abstract partial class TListPager<TEntity, TRepository, TSqlStrConstruct> : UserControl ...

  2. Dynamic CRM 2013 on Windows Azure VM

    三个A2 Size的VM,运行Windows Server 2012: AD Server,SQL Server,App Server 将AD Server的ip地址填写到Windows Azure虚 ...

  3. #define命令的一些高级用法

    =========================================================== define中的三个特殊符号:#,##,#@ ================= ...

  4. 《Troubleshooting SQL Server》读书笔记-CPU使用率过高(下)

    <Troubleshooting SQL Server>读书笔记-CPU使用率过高(下) 第三章 High CPU Utilization. CPU使用率过高的常见原因 查询优化器会尽量从 ...

  5. Jenkins安装plugin

    Jenkins支持很多的plugin,这些plugin极大地丰富了Jenkins的功能.安装plugin有两种方式:自动安装和手动安装. 1.自动安装 这种方式非常简单,但前提是Jenkins必须连接 ...

  6. Servlet容器模型(四)ServletConfig、ServletContext

    这两天刚刚回顾了一下servlet中的配置对象ServletConfig.上下文对象ServletContext.多线程相关的知识点,做了一下笔记,如下: 一.Servlet配置对象(ServletC ...

  7. Linux系统 nfs 共享及 挂载mount 配置

    要把 A服务器(192.168.1.1)的 /home 目录挂载到 B服务器(192.168.1.2)的/mnt目录 我们先在 A 服务器上编辑/etc/exports 文件, 添加下面这行代码 /h ...

  8. Android 中单位讲解

    1. dip(dp): device independent pixels(设备独立像素). dp也就是dip.这个和sp基本类似.如果设置表示长度.高度等属性时可以使用dp 或sp.但如果设置字体, ...

  9. RSA算法优化

    RSA算法优化 大数乘法 模乗优化 剩余定理(孙子定理) RSA加解密 python的RSA计算优化 #-*- coding: utf-8 -*- ''' /********************* ...

  10. IDEA异常解决: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    有时候解决问题不仅仅是解决问题.-----jstarseven 最近因为开发需要,需要搭建一个ssm开发框架,采用了开发工具IDEA. 整合完了SSM开发框架之后,发布的时候出现org.apache. ...