- 定义一个A数组用来存储结果
- 定义一个flag数组用来标记每组数据是否存在。
c++代码
#include #include #include #include #include using namespace std;int main() { int n, in; int A1 = 0, A2 = 0, A3 = 0, A5 = 0; double A4 = 0; int A4T = 0; cin >> n; int flag = 1; int A2T = 0; while (n--) { cin >> in; if (in % 5 == 0 && in % 2 == 0) //能被 5 整除的数字中所有偶数的和; A1 += in; else if (in % 5 == 1) { //被5整除后余1的数字交错求和,n1-n2+n3-n4... A2T++; in = in * flag; A2 += in; flag *= -1; } else if (in % 5 == 2) { //能被5整除余2的树的个数 A3++; } else if (in % 5 == 3) { //被5整除后余3的树的平均值,保留一位小数 A4 += in; A4T++; } else if (in % 5 == 4) { //被5整除余4中数字最大的数字 if (in > A5) A5 = in; } } if (A1 != 0) cout << A1 << " "; else cout << "N "; if (A2T == 0) cout << "N "; else cout << A2 << " "; if (A3 == 0) cout << "N "; else cout << A3 << " "; if (A4 == 0) cout << "N "; else printf("%.1f ", A4 / A4T); if (A5 == 0) cout << "N" << endl; else cout << A5 << endl; return 0;}复制代码
python3代码
def main(): s = input().split() num = list(map(int,s)) A = [0,0,0,0,0,0] flag = [1,0,0,0,0,0,0] for i in range(1,len(num)): if(num[i] % 10 ==0): A[1] += int(num[i]) flag[1] = 1 elif(num[i]% 5 == 1): A[2] += num[i]*flag[0] flag[0] *= -1 flag[2] = 1 elif(num[i]%5==2): A[3] += 1 flag[3] = 1 elif(num[i] % 5 == 3): A[4] += num[i] flag[4] = 1 A[0] += 1 elif(num[i] % 5 == 4): flag[5] = 1 if(num[i] > A[5]): A[5] = num[i] if(flag[1] == 1): print(A[1],end=' ') else: print('N',end= ' ') if(flag[2] == 1): print(A[2], end= ' ') else: print('N', end= ' ') if(flag[3] == 1): print(A[3],end= ' ') else: print('N',end= ' ') if(flag[4] == 1): print(round(float(A[4])/A[0],1),end= ' ') else: print('N',end= ' ') if(flag[5] == 1): print(A[5]) else: print('N')main()复制代码