HDU1013 - Digital Roots (模拟)
来源:互联网 发布:设计彩盒用什么软件 编辑:程序博客网 时间:2024/06/05 10:11
题目链接
- 思路
- 代码
- 利用定理
- 代码
思路
求解数字根,要注意的是数据是大数,需要处理。或者根据定理来进行求解。
代码
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){ int num, ans; char str[1000]; while(scanf("%s", str) && str[0]!='0') { num = 0; // 处理过一次后的数据便可以用 int 存下来 for(int i=0; str[i]!='\0'; i++) num += (str[i] - '0'); while(num>9) { ans = 0; while(num) { ans += num%10; num /= 10; } num = ans; } printf("%d\n", num); } return 0;}
利用定理
另一种解法,wiki上的解释。
整理就是
代码
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){ int num; char str[1000]; while(scanf("%s", str) && str[0]!='0') { num = 0; for(int i=0; str[i]!='\0'; i++) num += (str[i] - '0'); printf("%d\n", 1+(num-1)%9); } return 0;}
0 0
- HDU1013 - Digital Roots (模拟)
- hdu1013(Digital Roots)
- HDU1013 Digital Roots 模拟&数论(我不会数论)
- hdu1013 Digital Roots (模拟||九余数定理)
- HDU1013 POJ1519 Digital Roots(解法二)
- HDU1013 POJ1519 Digital Roots(解法三)
- HDU1013 Digital Roots
- hdu1013 Digital Roots
- hdu1013 Digital Roots
- HDU1013 Digital Roots
- hdu1013-Digital Roots
- hdu1013 Digital Roots
- hdu1013 Digital Roots
- hdu1013:Digital Roots
- hdu1013 Digital Roots 数论
- hdu1013 Digital Roots
- HDU1013 Digital Roots
- HDU1013 Digital Roots
- JVM(一):体系结构 && 内存模型
- AOP和IoC的实现原理
- Android 微信支付
- 开源框架Android Asynchronous Http Client
- 开源搜索引擎评估:lucene sphinx elasticsearch
- HDU1013 - Digital Roots (模拟)
- 第八章 假设检验
- python用到CMD的几个文件夹操作命令
- ArcGIS for Android 在Eclipse上的安装配置 (上:下载篇)
- ArcGIS中的三种查询
- 电脑常用端口对照表
- 快速搭建Android开发环境
- Python之yield
- iOS 多个引用库文件冲突