zoey
点击国内主页可以让浏览速度加快哦 ~ !

关于HDOJ 1013题(digital Roots)的理解(C/C++)

2019-07-31 hdoj int型的数的长度计算法方法 string和char型的长度 九余数定理以及弃九法
Word count: 1.1k | Reading time: 5min

最开始由于题目没说细具体的数字的位数,我还说想着不用string类型,直接用一个int类型的数来装装看,结果毫无意外地wrong answer了。

题目描述

Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 101376 Accepted Submission(s): 31506

Problem Description
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

Source
Greater New York 2000

原题链接

More info:Question

int型数的长度

如最开始所说的,本意是想着应该不会有大数让算,直接用int型试试?(理想很丰满,现实很骨感)果不其然地出错了……

以下是出错的代码(除了不能适用于大数,我感觉好像是没毛病的(当然,有其他问题的话欢迎各位大佬的指正,一定虚心接受并改正)):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include<iostream>

using namespace std;
int root_(int x) {
int a = x,res=0;
while (a) {
res += (a % 10);
a /= 10;
}
if(res<10) return res;
return root_(res);
}
int main() {
int x;
while (cin >> x && x) {
cout <<root_(x)<< endl;
}
return 0;
}

为毛把错误代码贴出来呢?其一,是为了警示自己,下次别再犯这种侥幸错误了;其二嘛,是感觉这个while循环里的代码其实可以有其他的应用,以后如果遇到的话可以采用下。例如,让算一个int整数一共有多少位,就可以用上述的root_()函数里的部分代码来实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include<iostream>

using namespace std;
int f(int x) {
int a = x,res=0;
while (a) {
res++;
a /= 10;
}
return res;
}
int main() {
int x;
while (cin >> x && x) {
cout <<f(x)<< endl;
}
return 0;
}

运行结果示意:

九余数定理以及弃九法

“弃九法”也叫做弃九验算法,利用这种方法可以验算加、减计算的结果是否错误。把一个数的各位数字相加,直到和是一个一位数(和是9,要减去9得0),这个数就叫做原来数的弃九数。

然而感觉本题中的Digital Root就是弃九数(如果我的理解有问题,还请大家帮忙指出纠正!)

九余数定理:
一个数对九取余,得到的数称之为九余数;
一个数的九余数 等于 它的各个数位上的数之和的九余数!

但是,如果直接对9取余,那么有整除的情况出现,但是除非一个数是0,否则这个数的Digital Root不应该出现0才对
于是,可以用(各位数之和-1)%9+1的方式来求一个数的弃九数(Digital Root)

关于string的长度函数

关于string的长度:
头文件为 < string > 时,用 string a的形式,
int len=< string >.size();

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<string>
using namespace std;
int main() {
string a;
while (cin >> a) {
int len = a.size();
cout << len << endl;
}
return 0;
}

头文件为< cstring >时,用char a[]的形式,
int len=strlen(< string >);

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char a[100];
while (cin >> a) {
int len = strlen(a);
cout << len << endl;
}
return 0;
}

两个代码都可以输出当前输入的字符串的长度

Accepted代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<string>
using namespace std;
string a;
int main() {
int sum = 0;
while (cin >> a && a != "0") {
sum = 0;
for (int i = 0; i < a.size(); i++) {
sum += (a[i] - '0');
}
cout << (sum - 1) % 9 + 1 << endl;
}
}

Author: Zoey

Link: https://zoey1038569979.github.io/2019/07/31/blog3/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
看板娘测试博
NextPost >
关于HDOJ 1012题的理解(C/C++)
CATALOG
  1. 1. 题目描述
  2. 2. 原题链接
  3. 3. int型数的长度
  4. 4. 九余数定理以及弃九法
  5. 5. 关于string的长度函数
  6. 6. Accepted代码