题干:

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

解题报告:

刚开始直接开longlong,wa了。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
char s[100000];
int b[100000];
int get(ll x) {int p = 0;while(x) {b[++p] = x%10;x/=10;}   return p;
}
int main()
{while(~scanf("%s",s)) {if(s[0] == '0') break;ll ans = 0;for(int i = 0; i<strlen(s); i++) ans += 1LL * (s[i] - '0');while(1) {int p = get(ans);if(p == 1) break;ans = 0;for(int i = 1; i<=p; i++)ans += 1LL * b[i];}printf("%d\n",b[1]);}return 0;
}

【HDU - 1013 】Digital Roots (大数模拟)相关推荐

  1. hdu 5568(dp+大数模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5568 官方题解: #include <cstdio> #include <cstri ...

  2. hdoj 1013 Digital Roots

    链接:zoj 1115 或 hdoj 1013 或poj 1519 虽说是水题,却几经波折才搞定.该题目中的数字可能非常大,所以不能使用整型数,只能采用字符变量 代码如下: #include < ...

  3. HDU1013 POJ1519 Digital Roots(解法二)

    该问题的最佳解法是利用数论的9余数定理来计算数根.一个数的数根等于该数的9的余数,若余数为0则结果为9. 问题链接:HDU1013 POJ1519 Digital Roots.入门练习题,用C语言编写 ...

  4. HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】

    问题链接:HDU1163 Eddy's digital Roots. 问题简述:参见上述链接. 问题分析:计算n^n的数根,一要快,二要简单.使用快速模幂计算,加上数论中的九余数定理就完美了. 程序说 ...

  5. HDU1013 POJ1519 Digital Roots

    该问题的最佳解法是利用数论的9余数定理来计算数根.一个数的数根等于该数的9的余数,若余数为0则结果为9. 问题链接:HDU1013 POJ1519 Digital Roots.基础训练题,用C语言编写 ...

  6. HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】(废除!!!)

    本文废除,参见下述链接. 参考链接:HDU1163 Eddy's digital Roots[快速模幂+九余数定理+水题] 问题链接:HDU1163 Eddy's digital Roots. 问题简 ...

  7. HDU1013 POJ1519 Digital Roots(解法三)【废除!!!】

    本文废除!!! 参见链接:HDU1013 POJ1519 UVALive2085 Digital Roots[数根+水题] 该问题的最佳解法是利用数论的9余数定理来计算数根.一个数的数根等于该数的9的 ...

  8. HDU1013 POJ1519 Digital Roots(解法二)【废除!!!】

    本文废除!!! 参见链接:HDU1013 POJ1519 UVALive2085 Digital Roots[数根+水题] 该问题的最佳解法是利用数论的9余数定理来计算数根.一个数的数根等于该数的9的 ...

  9. HDU1013 POJ1519 Digital Roots(解法三)

    该问题的最佳解法是利用数论的9余数定理来计算数根.一个数的数根等于该数的9的余数,若余数为0则结果为9. 问题链接:HDU1013 POJ1519 Digital Roots.入门练习题,用C语言编写 ...

  10. Digital Roots

    题目的链接为: http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 题目为: Dig ...

最新文章

  1. 将spark默认日志log4j替换为logback
  2. 【前端统计图】echarts改变颜色属性的demo
  3. 外链引入css有哪些方式_快速收录的外链操作技巧
  4. 一起学习C语言:结构体(一)
  5. (62)FPGA一维数组(reg)
  6. 我离baidu.com有几跳
  7. python 员工考勤_用python写的考勤自动打卡程序
  8. HBase集成(准备篇)
  9. Qt实现 QQ好友列表QToolBox
  10. 移动磁盘拒绝访问要如何办啊
  11. android4.1.1+nexus S安装BT5
  12. ORACLE导出表数据-dmp文件
  13. 软件特性开发的流程感想
  14. 【打卡-Coggle竞赛学习2023年1月】文本相似度匹配
  15. LPC1768处理器开发板下载软件的使用方法
  16. TOM、腾讯、网易|你了解大厂企业邮箱的登陆入口吗?
  17. 《Java 并发编程的艺术》迷你书
  18. Springboot+Vue实现富文本发表文章功能
  19. NVIDIA GeForce Go 6100 for Wndows 7 (笔记本)
  20. 《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐

热门文章

  1. [Leetcode][第214题][JAVA][最短回文串][KMP][RK]
  2. [Leetcode][第108题][JAVA][将有序数组转换为二叉搜索树][二分法][递归]
  3. K-Dominant Character CodeForces - 888C 思维 构造
  4. HDU-1251 统计难题 map写法
  5. 计算机工程与应用单像素成像,2011计算机工程与应用基于压缩感知理论的单像素成像系统研究_白凌云.pdf...
  6. textarea回车不换行 小程序_微信小程序商城到底值得不值得开通?
  7. bootstrap 两个轮播图冲突_为什么使用bootstrap在一个页面同时做两个轮播效果时,只有第一个有效??...
  8. c++ 结构体初始化_STM32入门系列-使用库函数点亮LED,LED初始化函数
  9. 建筑电气工程设计常用图形和文字符号_怎么看懂建筑电气工程图?用最直白方式,教你基础识图!超级详细...
  10. 实例解析linux内核I2C体系结构(2)