PTA 乙级 1012 数字分类 (20分)(测试点7无法通过)

 很奇怪啊,测试点7一直无法通过

测试点8若无法通过,原因可能是没有考虑A2可能为0情况(进行过A2的计算,但和初始化0重复),利用标志位进行解决

 1 #include<iostream>
 2 #include<vector>
 3 #include<cmath>
 4 #include<iomanip>
 5 
 6 using namespace std;
 7 
 8 int main() {
 9     /*正整数个数*/
10     int n = 0;
11     /*A1~A5(A3除外)*/
12     int num[5] = { 0 };
13     /*判断A2是否计算的标志位*/
14     int temp = 0;
15     /*输出标志位*/
16     int flag = 0;
17     /*A2、A4计数*/
18     int c = 0, j = 0;
19     cin >> n;
20     int a[1000] = { 0 };
21 
22     //输入
23     for (int i = 0; i < n; i++) {    
24         cin >> a[i];
25     }
26     for (int i = 0; i < n; i++) {
27         if (a[i] % 10 == 0)            //A1的计算
28             num[0] += a[i];
29         else if (a[i] % 5 == 1) {    //A2的计算
30             num[1] += (pow(-1, j) * a[i]);
31             temp = 1;    
32             j++;
33         }
34         else if (a[i] % 5 == 2)        //A3的计算(算总和)
35             num[2]++;
36         else if (a[i] % 5 == 3) {    //A4的计算
37             num[3] += a[i];
38             c++;
39         }
40         else if (a[i] % 5 == 4 && a[i] > num[4])    //A5的计算
41             num[4] = a[i];
42     }
43 
44     //进行输出
45     for (int i = 0; i < 5; i++) {
46         if (flag == 1)
47             cout << " ";
48         if (num[i] == 0) {
49             if (i == 1 && temp == 1) { cout << num[i]; temp = 0; continue; }    //判断是否进行了A2的计算
50             cout << "N";
51         }
52         else {
53             if (i == 3) {
54                 cout << setprecision(2) << num[3] * 1.0 / c;    //求平均数,限制小数点位数
55                 continue;
56             }
57             cout << num[i];
58         }
59         flag = 1;
60     }
61     
62     return 0;
63 }

测试点7很令人挠头,有的解析说是定义的数组没有初始化,PTA系统自动判错,但我的数组都初始化了,还是不对,有哪位大神指点一下?

猜你喜欢

转载自www.cnblogs.com/SCP-514/p/13211334.html