PAT乙级-1012


PAT乙级-1012


题目分析

 题目要求比较明确,switch···case···语句暴力解决即可。唯一一个需要注意的点是A4的计算。首先作为一个平均数A4应该定义为double型变量;其次当A4对应的数没有出现过的时候,计算平均数会出现除0的情况,因此需要对此进行单独判断。

 保留一位小数的实现已经在之前的题目中介绍过:@PAT甲级-1002

代码

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
#include "iostream"
#include "iomanip"
#define items 5
using namespace std;

int main(){
int n;
cin >> n;
int num, A1 = 0, A2 = 0, A3 = 0, cnt = 0, A5 = 0;
double A4 = 0;
bool sgn = false, flag[items];
for(int i=0; i<items; ++i)
flag[i] = false;
for(int i=0; i<n; ++i){
cin >> num;
switch (num % 5){
case 0:
if(num % 2 == 0){
A1 += num;
flag[0] = true;
}
break;
case 1:
if(sgn)
A2 -= num;
else
A2 += num;
sgn = !sgn;
flag[1] = true; break;
case 2:
A3++;
flag[2] = true; break;
case 3:
A4 += num;
++cnt;
flag[3] = true; break;
default:
if(num > A5)
A5 = num;
flag[4] = true; break;
}
}
if(cnt != 0)
A4 /= cnt;
for(int i=0; i<items; ++i){
if(flag[i] == true){
switch (i){
case 0:
cout << A1; break;
case 1:
cout << A2; break;
case 2:
cout << A3; break;
case 3:
cout << setiosflags(ios::fixed) << setprecision(1) << A4; break;
default:
cout << A5; break;
}
}else
cout << 'N';
if(i != 4)
cout << " ";
}
return 0;
}

转载 请注明来源:©Tinshine