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
 
 
用int 的话  会 结果错误!!
#include<iostream>
#include<cmath>
using namespace std;
void f(int a)
{
int s=;
for(int i=;;i++){
int t=pow((double),i);
if(a/t==)break;
s+=a%(*t)/t;
}
if(s/!=)f(s);
else cout<<s<<endl;
}
int main()
{
int n;
while(cin>>n){
if(n==)break;
f(n);
}
return ;
}

所以要用字符串!!

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void g(int a)
{
int s=;
for(int i=;;i++){
int t=pow((double),i);
if(a/t==)break;
s+=a%(*t)/t;
}
if(s/!=)g(s);
else cout<<s<<endl;
}
void f(string a)
{
int s=,n=a.length();
for(int i=;i<n;i++){
s+=a[i]-'';
}
if(s/!=)g(s);
else cout<<s<<endl;
}
int main()
{
string n;
while(cin>>n){
if(n=="")break;
f(n);
}
//system("pause");
return ;
}
 

K - Digital Roots(第二季水)的更多相关文章

  1. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  2. D - Counterfeit Dollar(第二季水)

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  3. I - Long Distance Racing(第二季水)

    Description Bessie is training for her next race by running on a path that includes hills so that sh ...

  4. Y - Design T-Shirt(第二季水)

    Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA ...

  5. N - Robot Motion(第二季水)

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  6. S - 骨牌铺方格(第二季水)

    Description          在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种, ...

  7. R - 一只小蜜蜂...(第二季水)

    Description          有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数.         其中,蜂房的结构如下所示.     ...

  8. E - Number Sequence(第二季水)

    Description A single positive integer i is given. Write a program to find the digit located in the p ...

  9. G - Reduced ID Numbers(第二季水)

    Description T. Chur teaches various groups of students at university U. Every U-student has a unique ...

随机推荐

  1. Xcode no visible @interface for xxx declares the selector errors

  2. ARM入门实践(一)----Mini6410上最简单的LED点灯裸机程序

    Mini6410上最简单的LED点灯裸机程序 : 实验环境: 根据友善教程,要用ADS,据说现在都不用这个了,但是为了打开友善给的mcp工程,就下了一个,Win7下弄上兼容模式和管理员权限,再下一个S ...

  3. poj1201/zoj1508/hdu1384 Intervals(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Intervals Time Limit: 10 Seconds      Mem ...

  4. 2015.4.2-SQL 简单语句(一)

    1.创建一个student表 create table student_masen( sno char(4) not null primarykey sname nchar(10) not null ...

  5. Ecstore 2.0 报表显示空白

    INSERT INTO `sdb_ectools_analysis` (`id`, `service`, `interval`, `modify`) VALUES (, ), (, ), (, ); ...

  6. 浅谈window.attachEvent

    以前写 JavaScript 脚本时,事件都是采用object.event = handler;的方式初始化.这种方式对于 Internet Explorer.Mozilla/Firefox 和 Op ...

  7. 检测.net framework 版本

    项目中,自己要制作asp.net项目的安装文件,由于项目依赖于.net framework 3.5 sp1,故需检测环境是否符合要求,才能安装程序 度娘找到检测方案:各.net版本对应的安装补录下都有 ...

  8. mysql----show slave status \G 说明

    show slave status \G 可以用来查看mysql 的复制状态,有些列名所表达的意思不太明确,现整理如下: 1. Slave_IO_State:ID线程的状态,如果master 的所有变 ...

  9. Qt直接使用OpenSSL里的函数

    简述 OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法.常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用. 简述 下载安装 使用 更多参考 下载安装 ...

  10. Linux下(主要针对Ubuntu)下桌面分辨率的添加

    系统版本: Linux (Ubuntu) 其他桌面发行版应该也行. 相关命令: lspci, cvt, xrandr 在桌面分辨率不正常显示桌面或者没有最佳的分辨率时,需要修改添加适合的桌面分辨率模式 ...