来源: POJ
编程题#1:年龄与疾病
注意: 总时间限制: 1000ms 内存限制: 65536kB
1.1 描述
某医院想统计一下某项疾病的获得与否与年龄是否有关,需要对以前的诊断记录进行整理。
输入
共2行,第一行为过往病人的数目n(0 < n <= 100),第二行为每个病人患病时的年龄。
输出
每个年龄段(分四段:18以下,19-35,36-60,大于60注意看样例输出的格式)的患病人数占总患病人数的比例,以百分比的形式输出,精确到小数点后两位(double)。关于c++的格式化的输入输出,请参考:http://www.cplusplus.com/reference/iomanip。也可以在网上搜索一下,资料很多的。
样例输入
10
1 11 21 31 41 51 61 71 81 91
样例输出
1-18: 20.00%
19-35: 20.00%
36-60: 20.00%
60-: 40.00%
1.2 提示
注意最后一行的输出是“60-: ”,而不是“61-: ”。
每个冒号之后有一个空格。
输出可以用 cout<<fixed<<setprecision(2) << f;
来保留f后面的两位小数。
1.3 解答
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n, score, tmp;
double per[4] = {0};
cin >> n;
tmp = n;
while (tmp--) {
cin >> score;
if (score > 0 && score <= 100) {
if (score <= 18)
per[0]++;
else if (score <= 35)
per[1]++;
else if (score < 60)
per[2]++;
else if (score == 60) {
per[2]++;
per[3]++;
} else
per[3]++;
}
}
for (int i = 0; i < 4; i++)
per[i] = per[i] / n;
cout << "1-18: " << fixed << setprecision(2) << per[0] * 100 << "%" << endl;
cout << "19-35: " << fixed << setprecision(2) << per[1] * 100 << "%" << endl;
cout << "36-60: " << fixed << setprecision(2) << per[2] * 100 << "%" << endl;
cout << "60-: " << fixed << setprecision(2) << per[3] * 100 << "%" << endl;
}
编程题#2:成绩判断
注意: 总时间限制: 1000ms 内存限制: 6000kB
2.1 描述
输入一个0–100的分数,判断分数代表什么等级。
95<=分数<=100, 输出1
90<=分数<95,输出2
85<=分数<90,输出3
80<=分数<85,输出4
70<=分数<80,输出5
60<=分数<70输出6
分数 < 60;输出7.
输入
n
输出
m
样例输入
87
样例输出
3
2.2 解答
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n, m;
cin >> n;
if (n >= 0 && n < 100){
if (n >= 95)
m = 1;
else if (n >= 90 && n < 95)
m = 2;
else if (n >= 85 && n < 90)
m = 3;
else if (n >= 80 && n < 85)
m = 4;
else if (n >= 70 && n < 80)
m = 5;
else if (n >= 60 && n < 70)
m = 6;
else
m = 7;
}
cout << m <<endl;
}
编程题#3:找出第k大的数
注意: 总时间限制: 1000ms 内存限制: 65536kB
3.1 描述
用户输入N和K,然后接着输入N个正整数(无序的),程序在不对N个整数排序的情况下,找出第K大的数。注意,第K大的数意味着从大到小排在第K位的数。
输入
N
K
a1 a2 a3 a4 … aN
输出
b
样例输入
5
2
32 3 12 5 89
样例输出
32
3.2 解答
设定b
很大(大于每一个输入的a[n]),并设定一个tmp
很小(小于每一个输入的a[n])。
然后重复K
次以下动作:找出大于tmp
且小于b
的值(也就是第m大的数,m代表执行次数),然后把这个数赋给b。
最后得到的就是第K大的数了。全过程没有涉及排序。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int a[999], b = 9999, N, K, *pa, tmp = 0;
cin >> N;
cin >> K;
pa = a;
for (int i = N; i > 0; i--) {
cin >> *pa;
pa++;
}
pa = a;
while (K--) {
for (int i = N; i > 0; i--) {
if (*pa > tmp && *pa < b)
tmp = *pa;
pa++;
}
b = tmp;
tmp = 0;
pa = a;
}
cout << b;
}
编程题#4:人民币支付
注意: 总时间限制: 1000ms 内存限制: 65536kB
4.1 描述
从键盘输入一指定金额(以元为单位,如345),然后输出支付该金额的各种面额的人民币数量,显示100元,50元,20元,10元,5元,1元各多少张,要求尽量使用大面额的钞票。
输入
一个小于1000的正整数。
输出
输出分行,每行显示一个整数,从上到下分别表示100元,50元,20元,10元,5元,1元人民币的张数
样例输入
735
样例输出
7
0
1
1
1
0
4.2 解答
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int money, hundred, fifty, twenty, ten, five, one, remain;
cin >> money;
if (money < 1000) {
hundred = (money - money % 100) / 100;
remain = money - hundred * 100;
fifty = (remain - remain % 50) / 50;
remain -= fifty * 50;
twenty = (remain - remain % 20) / 20;
remain -= twenty * 20;
ten = (remain - remain % 10) / 10;
remain -= ten * 10;
five = (remain - remain % 5) / 5;
one = remain - five * 5;
}
cout << hundred << endl << fifty << endl << twenty << endl << ten << endl << five << endl << one << endl;
}