Digital Roots
发布日期:2021-08-19 19:59:58 浏览次数:7 分类:技术文章

本文共 1722 字,大约阅读时间需要 5 分钟。

Digital Roots

My Tags (Edit)
Source : ACM ICPC Greater New York 2000
Time limit : 1 sec Memory limit : 32 M
Submitted : 5052, Accepted : 1337
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

很简单的题目,没有考虑到数据会给的很大,一直超出时间限定,用字符串解决就可以。
题目大意:将一个多位数每一位的数加起来,如果和还是多位数,则反复操作。

// 1080.cpp : 定义控制台应用程序的入口点。

//

include “stdafx.h”

int fuc(int num);

int _tmain(int argc, _TCHAR* argv[])

{
char digit;
int num = 0;
while ((digit=getchar())!=EOF)
{
if (digit == ‘\n’)
{
if (num == 0)
{

break;        }        printf("%d\n",fuc(num));        num = 0;    }    else    {        num += digit - '0';    }}return 0;

}

int fuc(int num)
{
int tmp = 0;
while (num>=10)
{
tmp += num % 10;
num /= 10;
}
tmp += num;
if (tmp < 10)
return tmp;
else
return fuc(tmp);

}

转载于:https://www.cnblogs.com/aprilvkuo/p/4367038.html

转载地址:https://blog.csdn.net/weixin_30951743/article/details/95894529 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:移动端前端笔记大全
下一篇:Java读写TXT文本

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年03月14日 07时44分24秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

qpython3安装lxml_在python的lxml中使用xml目录? 2019-04-21
java 幂取模_快速幂取模算法 2019-04-21
java build path jre_java-如何在安装了jre 7后为Jre 6设置路径? 2019-04-21
java上传下载源码_javaweb简单实现文件上传与下载源代码 2019-04-21
java socket udp 广播_1.Java 的屏幕广播(基于UDP),2.多线程下载器 2019-04-21
java控制热敏打印机的例子.rar_stm32控制热敏打印机 2019-04-21
java clone equals_(原)java中对象复制、==、equals 2019-04-21
java滚动字幕实训报告_Java实习报告 (7000字).doc 2019-04-21
php7 memcached.exe,PHP7 下安装 memcache 和 memcached 扩展 2019-04-21
计算机二级java技巧,计算机二级报java难考吗 2019-04-21
php foreach 数据库,php – 使用foreach将数据库检索的数据排列在HTML表中 2019-04-21
拉格朗日matlab编程例题,Matlab习题讲解.doc 2019-04-21
case是不是php语言关键字,PHP语言 switch 的一个注意点 2019-04-21
linux php mkdir失败,linux – mkdir错误:参数无效 2019-04-21
config.php渗透,phpMyAdmin 渗透利用总结 2019-04-21
java list 合并 重复的数据_Java ArrayList合并并删除重复数据3种方法 2019-04-21
android volley 上传图片 和参数,android - 使用android中的volley将图像上传到multipart中的服务器 - 堆栈内存溢出... 2019-04-21
android开发的取消清空按钮,Android开发实现带清空按钮的EditText示例 2019-04-21
android gp服务,ArcGIS Runtime SDK for Android开发之调用GP服务(异步调用) 2019-04-21
mysql整体会滚_滚mysql 2019-04-21