ZOJ Problem Set - 1115
来源:互联网 发布:前端怎么获取数据 编辑:程序博客网 时间:2024/04/28 06:40
ZOJ Problem Set - 1115
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=115
如果不用字符处理方式来得出第一次位数和的话会超时。
#include <iostream>
#include <string>
using namespace std;
int getNum(string s) {
int sSum = 0;
for(int i = 0;i < s.size();i++)
sSum += s[i] - '0';
return sSum;
}
int nSum(int n) {
if(10 > n) return n;
int sum;
while(0 == sum || 9 < sum) {
sum = 0;
while(0 < n) {
sum += n % 10;
n = n / 10;
}
n = sum;
}
return sum;
}
int main()
{
string str;
int num;
while(1) {
cin >> str;
if("0" == str) break;
num = getNum(str);
while(9 < num) {
num = nSum(num);
}
cout << num << endl;
}
return 0;
}
- ZOJ Problem Set - 1115
- ZOJ Problem Set - 1115
- ZOJ Problem Set - 1115 Digital Roots
- ZOJ Problem Set - 1115 Digital Roots
- ZOJ Problem Set - 1115||Digital Roots
- ZOJ Problem Set - 2060
- ZOJ Problem Set - 2972
- ZOJ Problem Set - 1037
- ZOJ Problem Set - 1048
- ZOJ Problem Set - 1049
- ZOJ Problem Set - 1051
- ZOJ Problem Set - 1067
- ZOJ Problem Set - 1151
- ZOJ Problem Set - 1205
- ZOJ Problem Set - 1025
- ZOJ Problem Set - 1029
- ZOJ Problem Set - 1076
- ZOJ Problem Set - 1117
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[转]
- ${abc}取值properties文件
- JDBC中的数据类型与日期问题
- marquee属性
- Android 登录Linux服务器[转]
- ZOJ Problem Set - 1115
- DELPHI中关于类的继承和多态问题
- CreateFont
- 初步试用Squid的替代产品──Varnish Cache网站加速器[转]
- Java中如何产生随机数
- 关于Java String,你知道多少?
- 数据存储与检索
- Linux不再海市蜃楼
- 用最简单命令杀死一个进程