赞
踩
给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:
输入格式:
每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。
输出格式:
对给定的 N 个正整数,按题目要求计算 A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。
若其中某一类数字不存在,则在相应位置输出 N。
输入样例 1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出样例 1:
30 11 2 9.7 9
输入样例 2:
8 1 2 4 5 6 7 9 16
输出样例 2:
N 11 2 N 9
#include <iostream> using namespace std; int main(){ int n, temp; int cnt[5] = {0}; int ans[5] = {0}; cin >> n; for (int i = 0; i < n; i++) { scanf("%d",&temp); if(temp % 5 == 0 && temp % 2 ==0){//A1 cnt[0]++; ans[0] += temp; }else if (temp % 5 == 1){//A2 if (cnt[1] % 2 != 1) { ans[1] += temp; }else{ ans[1] -= temp; } cnt[1]++; }else if (temp % 5 == 2){//A3 cnt[2]++; }else if (temp % 5 == 3){//A4 cnt[3]++; ans[3] += temp; }else if (temp % 5 == 4 ){//A5 if (temp > ans[4]) { ans[4] = temp; } cnt[4]++; } } for (int i = 0; i < 5; i++) { if (i != 0) { cout << " "; } if( cnt[i] != 0){ switch (i) { case 0:cout << ans[i]; break; case 1:cout << ans[i]; break; case 2:cout << cnt[i]; break; case 3:printf("%.1f",(double)ans[i]/cnt[i]); break; case 4:cout << ans[i]; break; } }else{ cout << "N"; } } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。