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

  1. #include <stdio.h>
  2.  
  3. int f(int t)
  4. {
  5. int s=;
  6. while(t>)
  7. {
  8. s+=t%;
  9. t/=;
  10. }
  11. return s;
  12. }
  13.  
  14. int main()
  15. {
  16. int n;
  17. while(scanf("%d",&n),n)
  18. {
  19. int i,j,t,s;
  20. t=n;
  21. if(t<)
  22. printf("%d\n",t);
  23. else if(t>=)
  24. {
  25. s=f(t);
  26. while(s>=)
  27. {
  28. s=f(s);
  29. }
  30. printf("%d\n",s);
  31. }
  32. }
  33. return ;
  34. }
  35. //wa

第二次做的:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char s[];
  5.  
  6. int f(int t)
  7. {
  8. int s=;
  9. while(t>)
  10. {
  11. s+=t%;
  12. t/=;
  13. }
  14. return s;
  15. }
  16.  
  17. int main()
  18. {
  19. while(scanf("%s",s)&&s[]!='')
  20. {
  21. int i,t,sum=;
  22. for(i=;i<strlen(s);i++)
  23. sum+=s[i]-'';
  24. if(sum<)
  25. printf("%d\n",sum);
  26. else if(sum>=)
  27. {
  28. t=f(sum);
  29. while(t>=)
  30. {
  31. t=f(t);
  32. }
  33. printf("%d\n",t);
  34. }
  35. }
  36. return ;
  37. }
  38. //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. LVS上DR和NAT模式的缺陷

    引言 相信一般的小公司用的最多的还是DR和NAT模式,关于DR和NAT模式的原理请看看下图,我们先从lvs的DR和NAT模式特性聊聊一些问题. 问题1.lvs的DR模式和NAT模式核心缺陷有哪些? D ...

  2. 【WIP】Rails Client Side Document

    创建: 2017/09/15 更新: 2019/04/14 删除其他语言的表述 更新: 2017/10/14 标题加上[WIP]  引入JavaScrpit/CSS  manifesto  n. 货单 ...

  3. [App Store Connect帮助]三、管理 App 和版本(2.7)输入 App 信息:添加 iMessage 信息版 App 的 App 信息

    您可以使用 Messages framework(Messages 框架)来创建贴纸包或 iMessage 信息版 App(可在 iMessage App Store 中获取).可作为独立 App,也 ...

  4. 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]

    [题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...

  5. BZOJ 2506 分块

    //By SiriusRen #include <bits/stdc++.h> using namespace std; ; ][],g[N],tmp=; struct Node{int ...

  6. BZOJ 1137 半平面交

    半平面交的板子 //By SiriusRen #include <bits/stdc++.h> #define double long double using namespace std ...

  7. 题解报告:hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者(巴什博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2188 Problem Description 对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每 ...

  8. ex41习题 41: 来自 Percal 25 号行星的哥顿人(Gothons)

    ex41习题 41: 来自 Percal 25 号行星的哥顿人(Gothons) 学习到本题卡住了,遇到一点费解的地方,mark一下.本题主要是介绍函数在字典这种数据类型中的应用,本实验在python ...

  9. Linux-fork()函数详解,附代码注释

    // // main.c // Project_C // // Created by LiJinxu on 16/8/13. // Copyright © 2016年 LiJinxu-NEU. All ...

  10. 【转】linux下passwd命令设置修改用户密码

    1.passwd 简单说明: 我们已经学会如何添加用户了,所以我们还要学习设置或修改用户的密码:passwd命令的用法也很多,我们只选如下的几个参数加以说明:想了解更多,请参考man passwd或p ...