HDU 1013 Digital Roots(字符串,大数,九余数定理)
Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 79180 Accepted Submission(s):
24760
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.
integers, one per line. The end of the input will be indicated by an integer
value of zero.
on a separate line of the output.
39
0
#include <stdio.h>
#include<string.h>
int main()
{
char a[];
int sum;
while(scanf("%s",&a)!=EOF)
{
if(strlen(a)==&&a[]=='')
break;
sum=;
for(int i=;i<strlen(a);i++)
{
sum=sum+a[i]-'';
if(sum>=)
sum=sum/+sum%;
}
printf("%d\n",sum);
}
return ;
}
/*因为输入的数字可能很长,而一个数的digital root和该数每一位的和的digital root相等,所以可以直接算出所输入的数字的和(不会超过范围),然后根据常规方法求digital root。
*/
#include <stdio.h>
#include <string.h> int root(int x)
{
int r=;
while (x)
{
r+=x%;
x/=;
}
if (r<)
return r;
else
return root(r);
} int main()
{
char c;
int r=;
while (c=getchar(),)
{
if (c=='\n')
{
if (r==)
break;
printf("%d\n",root(r));
r=;
}
else
{
r+=c-;
}
}
}
//9余数定理 !!
#include<stdio.h>
#include<string.h>
char a[];
int main()
{
int n;
while(~scanf("%s",a),strcmp(a,""))
{
int sum=;
int len=strlen(a);
for(int i=;i<len;++i)
sum+=a[i]-'';//字符化为数字
printf("%d\n",(sum-)%+);
}
return ;
}
HDU 1013 Digital Roots(字符串,大数,九余数定理)的更多相关文章
- HDU 1013 Digital Roots(字符串)
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
- HDU 1013 Digital Roots(to_string的具体运用)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1013 Digital Roots【字符串,水】
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1013.Digital Roots【模拟或数论】【8月16】
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
- HDU 1013 Digital Roots 题解
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- hdu 1013 Digital Roots
#include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...
- HDU OJ Digital Roots 题目1013
/*Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1310 - Digital Roots
先上题目: Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 杭电 1013 Digital Roots
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013 反思:思路很简单,但是注意各位数加起来等于10的情况以及输入0的时候结束程序该怎么去表达 #in ...
随机推荐
- LeetCode -- Linked List Circle ii
Question: Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- 【算法】分块——教主的魔法&不勤劳的图书管理员
由不勤劳的图书管理员带入了分块的坑,深深地被其暴力与优雅所征服.分块的实质就是将暴力块状封装起来,一整块的部分就一整块处理,零碎的部分就怎么暴力怎么来.因为分块大小的原因,限制了零碎部分数据的数量级, ...
- [Leetcode] Reorder list 重排链表
Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...
- MySQL使用笔记(七)排序和限制数据记录查询
By francis_hao Dec 17,2016 排序数据记录查询 排序是指将筛选出符合条件的数据进行有序排放,有升序(ASC(默认))方式和降序(DESC)方式. mysql> se ...
- 如何开始创建第一个基于Spring MVC的Controller
万事开头难,良好的开端是成功的一半! 以下示例怎么开始创建我们的第一个Spring MVC控制器Controller 1.新建一个java类,命名为:MyFirstController,包含以下代码, ...
- 运动目标前景检测之ViBe源代码分析
一方面为了学习,一方面按照老师和项目的要求接触到了前景提取的相关知识,具体的方法有很多,帧差.背景减除(GMM.CodeBook. SOBS. SACON. VIBE. W4.多帧平均……).光流(稀 ...
- Codeforces Round #478 C. Valhalla Siege
C. Valhalla Siege time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- java生成API文档
1.选择项目右键-Export\javadoc 2.选择生成工具在jdk安装目录下jdk\bin\javadoc.exe 3.在Eclipse里 export 选 JavaDoc,在向导的最后一页的E ...
- 组合数学--Polya 原理及典型应用
Redfield-Polya (Pólya enumeration theorem,简称PET)定理是组合数学理论中最重要的定理之一.自从 1927 年 Redfield 首次运用 group red ...
- [POI2014] KUR-Couriers(洛谷P3567)
洛谷题目链接:[POI2014]KUR-Couriers 题目描述 Byteasar works for the BAJ company, which sells computer games. Th ...