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. J20170618-hm

    番兵(ばんぺい) 哨兵 後始末 善后  

  2. Maya Calendar

    http://poj.org/problem?id=1008 按第一种记录方法算出总天数,然后按第二种记录方式输出. #include<stdio.h> #include<strin ...

  3. Unity 图形学 基础知识总结

    1. 渲染流水线     三大块:应用阶段,几何阶段,光栅化阶段                       渲染图元   顶点信息    GPU流水线     顶点数据=>     顶点着色器 ...

  4. Java使用Player播放mp3

    大家平时闲了都会听听歌,散散心,于是很多人就问,在Java里边如何播放歌曲呢,唉,别说,在Java里边还真能歌曲,下面我为大家揭晓. 我们都知道Java里边做什么都需要对应的jar包,首先贴上mave ...

  5. easyui -tree的详细讲解

    代码的具体实现 @{    ViewBag.Title = "人员查找";    ViewBag.LeftWidth = "200px";    ViewBag ...

  6. HTML-ul分分钟理解

    在HTML中,列表有三种,如图分别是有序.无序和自定义列表.上面是我在网络上找到的一张图片很明了就看以看出来,今天要分享的就是其中的无序列表Ul(unordered list),给大家整理了一下我所知 ...

  7. 使用Java生成word文档(附源码)

    当我们使用Java生成word文档时,通常首先会想到iText和POI,这是因为我们习惯了使用这两种方法操作Excel,自然而然的也想使用这种生成word文档.但是当我们需要动态生成word时,通常不 ...

  8. mongoDB的基本用法

    一.MongoDB初识 什么是MongoDB MongoDB是一个基于分布式文件存储的数据库.由c++语言编写.旨在为web应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库 ...

  9. ANDROID 开发之 SQLite

    SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...

  10. jQuery使用手册,【新手必备】

    jQuery是一款同prototype一样优秀js开发库类,特别是对css和XPath的支持,使我们写js变得更加方便!如果你不是个js高手又想写出优 秀的js效果,jQuery可以帮你达到目的!   ...