一、题目
给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:
- A1 = 能被 5 整除的数字中所有偶数的和;
- A2 = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n1−n2+n3−n4⋯;
- A3 = 被 5 除后余 2 的数字的个数;
- A4 = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;
- A5 = 被 5 除后余 4 的数字中最大数字。
输入格式:
每个输入包含 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;
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>
int main()
{
vector<int> array[5];
int max;
cin >> max;
int temp;
for(int i = 0; i < max; i++)
{
cin >> temp;
int n = temp % 5;
switch(n){
case 0:{
if(temp % 2 == 0){
array[0].push_back(temp);
}
break;
}
case 1:
array[1].push_back(temp);
break;
case 2:
array[2].push_back(temp);
break;
case 3:
array[3].push_back(temp);
break;
case 4:
array[4].push_back(temp);
break;
}
}
for(int i = 0; i < 5; i++)
{
//cout << array[i].size() << endl;
if(array[i].size() == 0)
{
if(i != 4)
cout << "N" << " ";
}else
{
switch (i){
//A1
case 0:
{
int num = 0;
for(vector<int>::iterator it = array[0].begin();it < array[0].end(); it++)
{
num += *it;
}
cout << num << " ";
break;
}
//A2
case 1:
{
int num1 = 0;
int i = 2;
for(vector<int>::iterator it = array[1].begin(); it < array[1].end(); it++,i++)
{
if(i % 2 == 0)
{
num1 += *it;
}else{
num1 -= *it;
}
}
cout << num1 << " ";
break;
}
//A3
case 2:
{
cout << array[2].size() << " ";
break;
}
//A4
case 3:
{
float num1 = 0;
for(int i = 0; i < array[3].size();i++)
{
num1 += array[3][i];
}
float num2 = array[3].size();
float total = num1 / num2;
cout << setprecision(2.0) << total << " ";
break;
}
//A5
case 4:
{
cout << *(max_element(array[4].begin(),array[4].end()));
break;
}
}
}
}
}
最后
以上就是大气万宝路最近收集整理的关于板栗说算法 之 PAT 乙级 1012 数字分类(测试点问题未解决)一、题目二、思路 三、代码实现的全部内容,更多相关板栗说算法内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复