Digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40786    Accepted Submission(s): 12584

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
 
第一次做的:(没考虑大数据的问题)

 #include <stdio.h>

 int f(int t)
{
int s=;
while(t>)
{
s+=t%;
t/=;
}
return s;
} int main()
{
int n;
while(scanf("%d",&n),n)
{
int i,j,t,s;
t=n;
if(t<)
printf("%d\n",t);
else if(t>=)
{
s=f(t);
while(s>=)
{
s=f(s);
}
printf("%d\n",s);
}
}
return ;
}
//wa

第二次做的:

 #include <stdio.h>
#include <string.h> char s[]; int f(int t)
{
int s=;
while(t>)
{
s+=t%;
t/=;
}
return s;
} int main()
{
while(scanf("%s",s)&&s[]!='')
{
int i,t,sum=;
for(i=;i<strlen(s);i++)
sum+=s[i]-'';
if(sum<)
printf("%d\n",sum);
else if(sum>=)
{
t=f(sum);
while(t>=)
{
t=f(t);
}
printf("%d\n",t);
}
}
return ;
}
//ac

链接(大神做法):http://www.cppblog.com/ArcTan/articles/167330.html

hdu1013(模拟&数论)

http://acm.hdu.edu.cn/showproblem.php?pid=1013

这个题模拟也可以AC,刚开始我也是模拟AC的。不过看了百度看了大牛的博客,感谢大牛,知道了还有数论这回事。

n=0 1 2 3 4 5 6 7 8 9 10 11 12 13 ......... 100 101 102 103 ....
roots=0 1 2 3 4 5 6 7 8 9 1 2 3 4 .......1 2 3 4....
原来是以1.....9为循环节的。想想也是,每次增加1,那么层层迭代下来,最终当ans<10的时候也是每次增加了1。如此,可以归纳出
roots=(n-1)%9+1

注意输入的数字很大需要字符串读入,求和得n:

#include<stdio.h>
#include<string.h>
#include<math.h>

int main()
{
    int i,n,tmp;
    char a[];
    while (scanf("%s",&a)&&a[]!='')
    {
        n=;
        for (i=;i<strlen(a); i++)
        {
            n+=a[i]-;
        }
        printf("%d\n",(n-)%+);
    }
    return ;
}

hdu_1013_Digital Roots_201310121652的更多相关文章

随机推荐

  1. 【转】关于Java基础你不得不会的34个问题

    1. 面向对象和面向过程的区别 面向过程 优点: 性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;比如单片机.嵌入式开发.Linux/Unix等一般采用面向过程开发,性能是最重要的 ...

  2. 汇编程序45:检测点13.2 (loop指令的中断例程)

    安装程序: assume cs:code //loop指令的替代实现 code segment start: mov ax,cs mov ds,ax mov si,offset sub1 mov ax ...

  3. Codeforces 763A

    乍看之下感觉有点无从下手,,其实是个很简单的水题,陷入僵局 题目大意:给一棵树,树上每个节点都染色,问能否取下一个节点,使得剩余所有子树上的点的颜色都相同.能输出YES和取下的节点编号,否则输出NO. ...

  4. CSS实现两栏布局

    写在前面 两栏布局是指页面布局由主栏和边栏组成,是许多网页的布局方式,一般使用CSS去实现两栏布局. 实现两栏布局的方式有多种,这里采用四种比较常见的实现方式.主要是流体布局(liquid layou ...

  5. CSS布局——三栏布局

    说到三栏布局,很多都会提到圣杯布局和双飞翼布局这两个经典的三栏布局方式.于是,我在网上搜了一些相关资料,阅读并跟着代码敲了一遍,发现在处理三栏布局上,他们采用的都是两边栏固定,中间栏自适应的策略.在处 ...

  6. 图解TCP/IP笔记(3)——IP协议

    目录 IP协议 IP寻址 IP地址组成 IP地址分类 广播地址 子网掩码 全局地址和私有地址 IP协议 跨越不同数据链路,实现两端节点之间的数据包传输 数据链路:只负责某一个区间之间的通信传输 IP协 ...

  7. Android项目实战_手机安全卫士软件管家

    ###1.应用程序信息的flags 1. int flags = packageInfo.applicationInfo.flags2. 0000 0000 0000 0000 0000 0000 0 ...

  8. Hibernate 延迟加载剖析与代理模式应用

    本文来源于:http://www.ibm.com/developerworks/cn/java/j-lo-hibernatelazy/#icomments

  9. Java_Web三大框架之Hibernate+jsp+HQL分页查询

    分页查询无处不在.使用Hibernate+jsp+HQL进行分页查询. 第一步:编写房屋实体类和House.hbm.xml映射. /* * 房屋实体类 */ public class House { ...

  10. Python三方库xlrd,xlwd-Excel读写

    恩,我是翻译汪,主要内容来自http://www.python-excel.org/ 在xlrd,xlwt这两个库中,Excel的结构表示为workbook整个Excel对象,sheet工作表,row ...