Digital Roots
Background
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.
Example
Input
24
39
0
Output
6
3
意思是输入一个数,每个数各个数相加,如果小于10结束输出。大于10各位数再相加只到小于10.
用数学方法除9求余,能整除9的输出9
#include<iostream>
using namespace std;
int main()
{
int i,j;
cin>>i;
while(i!=0)
{
j=i%9;
if(j==0)
cout<<9<<endl;
else
cout<<j<<endl;
cin>>i;
}
return 0;
}
这个结果符合要求但是没通过,找到另外一种方法:用字符串来保存数字可以做到每个数字独立,且每个数字范围为0~9,而我一开始的想法就是输入一个数字的字符串数组,我计算所有数字的总和,然后再把这个和sum的每个数字再存入数字的字符串数组,后面的做法就跟前面一样了,而循环的终止条件就是总和sum<10.
#include<stdio.h>
int main()
{
int i,m;
char s[1000];
while(scanf("%s",s)==1&&s[0]!='0'){
for(m=i=0;s[i];i++)
m+=s[i]-'0';
printf("%d\n",m%9==0?9:m%9);
}
return 0;
}
这个代码可以通过
Digital Roots的更多相关文章
- Digital Roots 1013
Digital Roots 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:456 测试通过:162 描述 T ...
- Eddy's digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- Digital Roots 分类: HDU 2015-06-19 22:56 13人阅读 评论(0) 收藏
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1163 Eddy's digital Roots
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- ACM——Digital Roots
http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 Digital Roots 时间 ...
- HDOJ 1163 Eddy's digital Roots(九余数定理的应用)
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- Eddy's digital Roots(九余数定理)
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU1163 Eddy's digital Roots【九剩余定理】
Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 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【模拟或数论】【8月16】
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
随机推荐
- Linux最常用命令的小总结
目录及文件的基本操作: cd .. 切换到当前目录的上一级目录 cd 切换工作目录至当前用户的家目录 cd - 返回到上一个打开的目录(像遥控器上的切换键,切换到上一个播放的电视频道) ll -h ...
- Android 基于Android的手机邮件收发(JavaMail)之四(邮件的发送)
上一个邮件的接受,因为不当操作,保存未完成,一切东西都得从头开始那就先从邮件发送来吧. 先下我们做一个较为简单的邮件发送 以下是源代码:相信看过上篇文章所给连接的人,对于javamail应该都有了一定 ...
- JqGrid自定义的列
$("#gridTable").jqGrid({ //...其它属性 colModel: [ //...其它列 { name: 'dsource_alarm', index: 'd ...
- iOS -Swift 3.0 -for(循环语句用法)
// // ViewController.swift // Swift-循环语句 // // Created by luorende on 16/12/08. // Copyright © 2016年 ...
- Glacierskating测试记录
这个游戏本身已经很成熟了,要提什么建议的话也是吹毛求疵.... 不过个人来讲不是很喜欢这个游戏,喜欢程度排倒数第二吧....感觉游戏就是一个套路,掌握了套路就不好玩了.....优点是随时随地可以玩一把 ...
- [原创]java WEB学习笔记107:Spring学习---AOP切面的优先级,重用切点表达式
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- DEV全选多选小技巧
var v1 = bindingSourceBase.DataSource as DataTable; foreach (DataRowView v in v1.DefaultView) { v[&q ...
- CentOS下搭建LAMP环境详解
前言:在这里将介绍如何在CentOS下搭建LAMP环境(全部使用源码编译安装),用于web服务器开发. •LAMP: Linux + Apache + PHP + Mysql. •系统: CentOS ...
- 关于firewalld防火墙的使用
要想使用该防火墙,应该需要安装 networkmanager 并启动其服务.因为之前使用的是 netctl 提供的wifi-menu 来连接无线网络,导致安装networkmanager之后启动 Ne ...
- VB.NET中Form窗体运行时,按F1进入全屏状态
1.在KeyDown事件中添加: If e.KeyValue = 112 Then Me.WindowState = FormWindowState.Maximized End If 注:1.其中11 ...