PAT乙级1012

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840

题解

就比较简单,判断每个数字是哪种情况,然后进行相应的计算即可。

下面的代码中其实数组是不必要的,每取一个数字就可以直接进行相应计算。

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
// PAT BasicLevel 1012
// https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840

#include <iostream>
using namespace std;

int main()
{
// 数字个数
int n;
cin >> n;

// 获取数字
int* numArr=new int[n];
for(int i=0;i<n;++i){
cin >> numArr[i];
}

// 遍历数组,计算A1至A5
int a1=0,a1Count=0;
int a2=0,flag=1,a2Count=0;
int a3=0,a3Count=0;
double a4Sum=0,a4Count=0;
int a5=0,a5Count=0;
for(int i=0;i<n;++i){
switch(numArr[i]%5){
case 0:
if(numArr[i]%2==0){
a1+=numArr[i];
a1Count++;
}
break;
case 1:
a2+=flag*numArr[i];
flag=-flag;
a2Count++;
break;
case 2:
a3++;
a3Count++;
break;
case 3:
a4Sum+=numArr[i];
a4Count++;
break;
case 4:
if(numArr[i]>a5){
a5=numArr[i];
a5Count++;
}
break;
}
}

// 输出A1至A5
if(a1Count>0){
cout << a1 << ' ';
}else{
cout <<"N ";
}

if(a2Count>0){
cout << a2 << ' ';
}else{
cout << "N ";
}

if(a3Count>0){
cout << a3 << ' ';
}else{
cout << "N ";
}

if(a4Count>0){
printf("%.1lf ", a4Sum / a4Count);
}else{
cout << "N ";
}

if (a5Count > 0){
cout << a5;
}
else{
cout << 'N';
}

delete[] numArr;
//system("pause");
return 0;
}

作者:@臭咸鱼

转载请注明出处:https://www.cnblogs.com/chouxianyu/

欢迎讨论和交流!