我是靠谱客的博主 拉长红酒,这篇文章主要介绍《算法竞赛入门经典》第三章思考题,现在分享给大家,希望可以做个参考。

题目1(必要的存储量)

数组可以用来保存很多数据,但在一些情况下,并不需要把数据保存下来。下面哪些题目可以不借助数组,哪些必须借助数组?请编程实现。假设输入只能读一遍。
1. 输入一些数,统计个数。
2. 输入一些数,求最大值、最小值和平均数。
3. 输入一些数,哪两个数最接近。
4. 输入一些数,求第二大的值。
5. 输入一些数,求它们的方差。
6. 输入一些数,统计不超过平均数的个数。

代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h> #include <stdlib.h> void count() // 1题 { int n, ct = 0; while(1 == scanf("%d", &n)) ++ct; printf("You've inputted %d numbersn", ct); } void maxMinAverage() // 2题 { int n, max, min, sum = 0, ct = 0, first = 1; while(1 == scanf("%d", &n)) { if(first) { max = min = n; first = 0; } if(max < n) max = n; if(n < min) min = n; sum += n; ++ct; } printf("max:%d min:%d average:%.3fn", max, min, sum*1.0/ct); } void nearest() // 3题 { int nums[100], i = 0, j, k; while(1 == scanf("%d", &nums[i])) ++i; printf("%dn", i); int a = nums[0], b = nums[1], distance = abs(nums[0]-nums[1]); for(j = 0; j < i - 1; ++j) for(k = j + 1; k < i; ++k) if(abs(nums[j]-nums[k]) < distance) { distance = abs(nums[j]-nums[k]); a = nums[j]; b = nums[k]; } printf("two nearest numbers: %d %d, distance: %dn", a, b, distance); } void second() // 4题 { int max, second, n; scanf("%d%d", &max, &second); int t1 = max > second ? max : second; int t2 = max < second ? max : second; max = t1; second = t2; //printf("max:%d second:%dn", max, second); while(1 == scanf("%d", &n)) { if(n > max) { second = max; max = n; continue; } if(n > second && n != max) second = n; } printf("max:%d second:%dn", max, second); } void variance() // 5题 { int nums[100]; int ct = 0, n, i; double ave = 0.0, sum = 0.0, psum = 0.0; while(1 == scanf("%d", &n)) { nums[ct++] = n; sum += n; } ave = sum / ct; for(i = 0; i < ct; ++i) psum += (nums[i] - ave) * (nums[i] - ave); printf("variance: %.3fn", psum / ct); } void smallerThanAve() // 6题 { int nums[100]; int ct = 0, sct = 0, n, i; double ave = 0.0, sum = 0.0; while(1 == scanf("%d", &n)) { nums[ct++] = n; sum += n; } ave = sum / ct; for(i = 0; i < ct; ++i) if(nums[i] < ave) ++sct; printf("%d numbers smaller than average %fn", sct, ave); } int main() { //count(); //maxMinAverage(); //nearest(); //second(); //variance(); //smallerThanAve(); return 0; }

题目2(统计字符1的个数):

下面的程序意图在于统计字符串中字符1的个数,可惜有瑕疵:

复制代码
1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h> #define maxn 10000000 + 10 int main() { char s[maxn]; scanf("%s", s); int tot = 0; for(int i = 0; i < strlen(s); i++) if(s[i] == 1) tot++; printf("%dn", tot); }

该程序至少有3个问题,其中一个导致程序无法运行,另一个导致结果不正确,还有一个导致效率低下。你能找到它们并改正吗?

答案:

复制代码
1
2
3
4
5
6
7
8
9
10
#include <stdio.h> int main() { int tot = 0; char ch; while((ch = getchar()) != EOF) if(ch == '1') ++tot; printf("%dn", tot); }

最后

以上就是拉长红酒最近收集整理的关于《算法竞赛入门经典》第三章思考题的全部内容,更多相关《算法竞赛入门经典》第三章思考题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部