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. 57.部门职位管理 ExtJs 展示

    1.jobInfo.jsp <%@ page language="java" pageEncoding="UTF-8"%> <script t ...

  2. weak看iOS面试

    2013年 面试官:代理用weak还是strong? 我 :weak . 面试官:明天来上班吧 2014年 面试官:代理为什么用weak不用strong? 我 : 用strong会造成循环引用. 面试 ...

  3. 334 Increasing Triplet Subsequence 递增的三元子序列

    给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下:    如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,    ...

  4. Angular——数据绑定

    基本介绍 angularjs可以实现数据的双向绑定:(1)视图到模型的数据绑定,(2)模型到数据的绑定 基本使用 1.ng-model可以实现视图到模型的数据传输 2.{{name}}可以实现模型到视 ...

  5. mysql自动增长的有关问题,怎么恢复从1开始

    mysql自动增长的问题,如何恢复从1开始在一个表中我设置到autoid为自动增长列例如有如下数据 1 张三 男 202 王五 男 223 李四 男 254 陈大 男 19 现在我把 autoid=3 ...

  6. [Windows Server 2012] Filezilla安装方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:FileZ ...

  7. 检索COM类工厂中CLSID 为 {000209FF-0000-0000-C000-000000000046}的组件时失败, 原因是出现以下错误: 80070005

    主要问题原因是Word权限配置问题 解决方案: 控制面板-管理工具-组件服务-计算机-我的电脑-DCOM配置 在列表中找到microsoft word97-2003 document 右键选择属性,选 ...

  8. dos2unix xxx.sh

    前几天写了一个Linux的自动化脚本,没有使用变量,就是一些Linux命令的集合 今天试着把一些相同的字段提出来用变量表示,然后在Linux里运行,就发现一直在报错: : command not fo ...

  9. JS弹出子窗口

    目的 在一个主窗口中,点击一个链接, 弹出一个子窗口 , 父窗口保留 在子窗口中点击关闭, 关闭子窗口. 子窗口的位置位于屏幕的中间 实现 main.html <!DOCTYPE html> ...

  10. 在iOS项目中嵌入RN代码

    1:在项目跟目录下创建一个ReactComponent文件夹.目录结构如下: 2: 在ReactComponent文件夹下新建一个 package.json 文件 { "name" ...