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. IOS Block 反向传值

    1.在需要像上一个界面传值的.h 文件实现代理方法 @property (nonatomic, copy) void(^isOpenHandler)(BOOL) ; 2.在执行操作的时候需要江操作的结 ...

  2. 从汇编看c++中含有虚基类对象的析构

    c++中,当继承结构中含有虚基类时,在构造对象时编译器会通过将一个标志位置1(表示调用虚基类构造函数),或者置0(表示不调用虚基类构造函数)来防止重复构造虚基类子对象.如下图菱形结构所示: 当构造类B ...

  3. postman接口测试工具3.0版本的坑

    今天用postman接口测试工具3.0版本被坑,找了半天,原来postman这个新版本有个坑啊 下面的get参数,第一行不管你填不填,都是无效的,可能是postman的一个bug吧

  4. activiti笔记三 Activiti问题重现

    测试的时候出现一个异常: ContextLoader.initWebApplicationContext(308) | Context initialization failed org.spring ...

  5. PHP ReflectionClass

    <?php /** * @desc test reflectionclass * @author songweiqing * @create_time 2015-01-7 * */ class ...

  6. python运维开发(十九)----Django后台表单验证、session、cookie、model操作

    内容目录: Django后台表单验证 CSRF加密传输 session.cookie model数据库操作 Django后台Form表单验证 Django中Form一般有2种功能: 1.用于做用户提交 ...

  7. NAS4Free 安装配置 -- 目录

    淘了个DIY的NAS主机,装了3块硬盘,安装配置NAS4Free,用来存储照片.电影等资料,并兼做下载机. 现在把拆箱.安装.配置过程记录下来,供有兴趣的同学参考. NAS4Free 安装配置(一)开 ...

  8. 使用ARM模板部署自动扩展的Linux VMSS(2)

    12.准备完了模板文件,我们使用Powershell来创建VMSS for Linux的自动扩展集合,首先登陆到Azure中国的ARM账号: Login-AzureRmAccount -Environ ...

  9. js 创建对象

    1.工厂模式 function createPerson(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.j ...

  10. Android Studio 调试过程中快捷查看断点处变量值(Ctrl+Shift+I无效)?

    当你在做Keymap到Eclipse后,在debug过程中,在Eclipse中我们很喜欢用Ctrl+Shift+I去查看一个运算或者调用的结果,这样用起来很方便.但是keymap到Eclipse后,你 ...