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. shell脚本-循环选择语句

    shell脚本-循环选择语句 过程式编程语言: 顺序执行 选择执行 循环执行 注:条件中的变量,可以在执行语句中使用,不用在加上"$". if语句 根据命令的退出状态来执行命令 单 ...

  2. 如何自学编程,零基础适合学习Java或者Web前端吗,非科班的能学java吗?

    很多人都会选择自学Java或者前端,相信有在校的大学生,有正在上班想转行的,也有已经在自学的.下面通过几个问题我来给大家分析学习编程的难易程度. 编程是什么? 通俗的理解,编程就是编写程序,哪什么是程 ...

  3. 看无可看 分治FFT+特征值方程

    题面: 看无可看(see.pas/cpp/c) 题目描述 “What’s left to see when our eyes won’t open?” “若彼此瞑目在即,是否终亦看无可看?” ---- ...

  4. SPOJ 1811 SAM 初探

    思路: 一个串建SAM 另一个串在SAM上跑 //By SiriusRen #include <cstdio> #include <cstring> #include < ...

  5. [转]android使用全局变量的两种方法

    本文转自:http://blog.csdn.net/feiyangxiaomi/article/details/9966215 在我们使用android编写程序的时候,少不了想利用全局变量,但是面向对 ...

  6. PHP开发之旅-表单验证

    一.创建表单 <form name = "login" method = "post" action="contact.php?action=l ...

  7. mysql下载和安装Windows服务

    一.下载mysql:https://dev.mysql.com/downloads/mysql/,解压拷贝到D:\software\mysql-8.0.13-winx64 二.在D:\software ...

  8. 使用 Spring Social 连接社交网络

    Spring Social 框架是spring 提供社交平台的分享组件 https://www.ibm.com/developerworks/cn/java/j-lo-spring-social/

  9. (原创)HyperPacer使用技巧之集合点设置

    版权声明:本文为原创文章,转载请先联系并标明出处 性能测试中,我们可以模拟最真实的用户操作来建立性能模型,但是这种模拟是相对的.譬如12306网站春运开始后每一天都是高峰,这种高负载情况会持续一至两个 ...

  10. [文章转载]-我的Java后端书架-江南白衣

    我的Java后端书架 (2016年暮春3.0版) 04月 24, 2016 | Filed under 技术 书架主要针对Java后端开发. 3.0版把一些后来买的.看的书添补进来,又或删掉或降级一些 ...