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. PHP学习笔记二十八【抽象类】

    <?php //定义一个抽象类.主要用来被继承 //如果一个类继承了抽象类,则它必须实现该抽象类的所有抽象方法(除非它自己也是抽象类) // abstract class Animal{ pub ...

  2. CSS 设计彻底研究(一)(X)HTML与CSS核心基础

    第1章 (X)HTML与CSS核心基础 这一章重点介绍了4个方面的问题.先介绍了 HTML和XHTML的发展历程以及需要注意的问题,然后介绍了如何将CSS引入HTML,接着讲解了CSS的各种选择器,及 ...

  3. javaScript表单焦点自动切换

    ---恢复内容开始--- <html> <head> <script> window.onload=function(){ var form=document.ge ...

  4. uva 12100 Printer Queue

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  5. (原+转)简明 Python 教程:总结

     简明 Python 教程 说明:本文只是对<简明Python教程>的一个总结.请搜索该书查看真正的教程. 第3章 最初的步骤 1. Python是大小写敏感的. 2. 在#符号右面的内容 ...

  6. python 核心编程第六章课后题自己做的答案

    6–6. 字符串.创建一个 string.strip()的替代函数:接受一个字符串,去掉它前面和后面的 空格(如果使用 string.*strip()函数那本练习就没有意义了) 'Take a str ...

  7. 用lsb_release -a 查看linux版本

    1.要通过yum 安装上这个命令的软件包 yum -y install redhat-lsb 2.lsb_release -a 查看linux版本信息

  8. mysql死锁——mysql之四

    1.MySQL常用存储引擎的锁机制 MyISAM和MEMORY采用表级锁(table-level locking) BDB采用页面锁(page-level locking)或表级锁,默认为页面锁 In ...

  9. ./configure : /bin/sh^M : bad interpreter

    用命令行来编译Qt的时候发生标题尚的错误. 原因是文件中带有DOS行结束符,必须把它转换成UNix结束符 references: http://stackoverflow.com/questions/ ...

  10. jquery validationEngine的使用

    1.引入文件 <script src="/js/jquery-1.4.2.min.js" type="text/javascript"></s ...