hdoj 1013Digital Roots
/*Digital Roots
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*/
<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
int main()
{
int sum;
char a[1000],n[1000];
while(gets(n))
{
if (strcmp(n,"0")==0)
{
break;
}
else
{
int sum=0;
for (int j=0;j<strlen(n);j++)
{
sum+=(n[j]-48);
}
while (sum>=10)
{
sprintf(a,"%d",sum);//把格式化的数据写入某个字符串缓冲区。 意思是把sum转化为字符串a。 sum=0;
for (int i=0;i<strlen(a);i++)
{
sum+=(a[i]-48);
}
}
printf("%d\n",sum);
}
}
}</span>
这种方法不easy想到。
<span style="font-size:18px;">//9余数定理
#include<stdio.h>
#include<string.h>
char a[10010];
int main()
{
int n;
while(~scanf("%s",a),strcmp(a,"0"))
{
int sum=0;
int len=strlen(a);
for(int i=0;i<len;++i)
sum+=a[i]-'0';
printf("%d\n",(sum-1)%9+1);//为什么减一后再加一。是为了避免18这些数字。
}
return 0;
}
</span>
hdoj 1013Digital Roots的更多相关文章
- HDU——1013Digital Roots(九余数定理)
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1163 Eddy's digital Roots(九余数定理的应用)
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- HDOJ 1013题Digital Roots 大数,9余数定理
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- HDOJ 1163 Eddy's digital Roots 九余数定理+简单数论
我在网上看了一些大牛的题解,有些知识点不是太清楚, 因此再次整理了一下. 转载链接: http://blog.csdn.net/iamskying/article/details/4738838 ht ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- hdoj 1385Minimum Transport Cost
卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...
随机推荐
- 怎样使windows上的javaWEB项目公布到Centos上
首先在windows上把项目导入到myeclipse或者eclipse(JEE)版本号上. 然后经过调试,没有错误后. 点击项目,然后右键导出(Export...) 然后选择JEE的war格式,这个是 ...
- Nginx 做系统的前端反向proxy
Nginx是一款很优秀的基于event的webserver.吞吐量大.占用资源少,只是文档就很让人郁闷了,免费的Nginx和收费的Nginx+的文档共用一份,配置完之后才发现免费的Nginx启动某些命 ...
- do while 循环和while循环的差别
do while 循环和while循环的差别 1.do while循环是先运行循环体,然后推断循环条件,假设为真,则运行下一步循环,否则终止循环. while循环是先推断循环条件,假设条件为真则 ...
- [Golang] 从零開始写Socket Server(3): 对长、短连接的处理策略(模拟心跳)
通过前两章,我们成功是写出了一套凑合能用的Server和Client,并在二者之间实现了通过协议交流.这么一来,一个简易的socket通讯框架已经初具雏形了,那么我们接下来做的.就是想办法让这个框架更 ...
- 敏捷开发 —— TDD(测试驱动开发)
测试驱动开发 TDD(Test-Driven Development)是敏捷开发的一项核心实践,同时也是一种设计技术和方法. 既然是测试驱动,便是测试,测试用例先行: 首先编写好测试用例,期待值,实际 ...
- Vue读书笔记:关于$ref、props和$emit
1.props实现父组件向子组件传递数据 子组件可以通过props接收到来自父组件的数据,并且是单向绑定的.也就是说,数据不能从子组件反向传递. 2.$ref实现子组件向父组件通信 来自官方非常难理解 ...
- tomcat开启https服务
一.创建证书 证书是单点登录认证系统中很重要的一把钥匙,客户端于服务器的交互安全靠的就是证书:本教程由于是演示所以就自己用JDK自带的keytool工具生成证书:如果以后真正在产品环境中使用肯定要去证 ...
- 懒人npm运行和打包命令
源码: @echo off&setlocal EnableDelayedExpansion & color 3e :start title npm 常用命令 by:ceet@vip.q ...
- ZBrush中Layer层笔刷介绍
本文我们来介绍ZBrush®中的Layer层笔刷,该笔刷是一种类似梯田效果的笔刷,常用来制作鳞甲和花纹图腾.他还可以用一个固定的数值抬高或降低模型的表面,当笔刷在重合时,笔画重叠部分不会再次位移,这使 ...
- day05-3 初步了解数据类型
目录 数据类型介绍 数字类型 整形(int) 浮点型(float) 字符串 列表 字典 布尔值 数据类型介绍 不同的数据会有不同的数据类型 为了定义不同的数据,我们Python中提供了下述几个数据类型 ...