HDU 1013 Digital Roots 数字根 字符串处理基础题
来源:互联网 发布:购物类网站数据库设计 编辑:程序博客网 时间:2024/05/21 05:05
题意:给你一个整数(骗你的),让你求它的数字根(经过处理后只剩下的个位数),如果不是个位数就要循环一个操作:把各个位的数加起来组合成一个新数,直到这个数是一个个位数(即题目所说的数字根)。
这题其实不难,题目也短,还好理解,然后正常人都很快就用一个int就写完并开心地交上去了。
恭喜你得到了一个WA。
然后你反复检查自己的代码,觉得,没毛病啊,int范围不够?改个long int 或 long long int 交上去,太棒了,还是一个WA。
这时候你就基本该想到了,这是一个超过long long int 范围的大整型,和1002题目一样考的是字符串处理。
估计这个串的规模在1000左右,出题人的目的达到了,不会在串的长度上为难人的,毕竟是道基础题。
杭电传送器
以下是我的AC代码:
#include <iostream>#include <string>using namespace std;int main(){ //如果用int 或 long long int 都只能得到WA,但你又感觉自己的代码符合题意 //这就意味着不是你思路错,而是题目规模,那么短的题目肯定有坑 string str; //用string类定义字符串时不写一维长度 while(cin >> str) { if(str[0]=='0') //输入0结束 break; else //9*1000都还没超过整型,估计这题的串规模在1000左右 { int n=0,a; for(int i=0;str[i]!='\0';i++) //把串的和变为整型存到n n+=str[i]-'0'; while(n>=10) //对n进行处理知道n是个位数(即题目说的数字根) { a=n; //用一个变量a来代替n进行下面的处理 n=0; //先清零变量n while(a) //把a的各个位的数的和赋给n,循环到a==0的时候结束 { n+=a%10; //每次都加个位数 a/=10; //把a的个位除掉 } } cout << n << endl; //AC了,好开心! } } return 0;}
0 0
- HDU 1013 Digital Roots 数字根 字符串处理基础题
- hdu 1013 digital roots(数字根)
- HDU 1013 Digital Roots(两种方法,求数字根)
- Digital Roots数字根 12
- 算法百题005:数字根(Digital Roots)
- 数字根——Digital Roots
- Digital Roots 求一个数的数字根
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- HDU--1013--Digital Roots
- hdu 1013 Digital Roots
- hdu 5703 Desert
- JMeter学习笔记3-如何创建第一个JMeter测试用例
- 反射机制 有什么用
- 4412驱动-key 按键驱动
- 腾讯2017实习生面试(三轮)
- HDU 1013 Digital Roots 数字根 字符串处理基础题
- GitHub导入项目到Eclipse
- Java finalize方法使用
- phpstorm点滴
- ES
- LeetCode : K-diff Pairs in an Array
- es
- 清空数据库所有表的存储过程(没看懂)
- 这8种武器点亮程序员的个人品牌