题目描述
给定一系列正整数,请按要求对数字进行分类,并输出以下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”。
输入例子:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出例子:
30 11 2 9.7 9
#include<iostream>
using namespace std;
struct pd{
int item=0;
bool used=0;
};
bool isOdd5(int x){
if(x%2==0&&x%5==0)
return 1;
return 0;
}
bool isx51(int x){
if(x%5==1)
return 1;
return 0;
}
bool isx52(int x){
if(x%5==2)
return 1;
return 0;
}
bool isx53(int x){
if(x%5==3)
return 1;
return 0;
}
bool isx54(int x){
if(x%5==4)
return 1;
return 0;
}
int main(){
int n,m=-1,COUNT=0;
cin>>n;
pd A1,A2,A3,A4,A5;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n;i++){
if(isOdd5(arr[i])){
A1.item+=arr[i];
A1.used=1;
}
if(isx51(arr[i])){
A2.item=A2.item+arr[i]*(-1)*m;
m*=-1;
A2.used=1;
}
if(isx52(arr[i])){
A3.item++;
A3.used=1;
}
if(isx53(arr[i])){
COUNT++;
A4.item+=arr[i];
A4.used=1;
}
if(isx54(arr[i])){
if(A5.item<arr[i]) A5.item=arr[i];
A5.used=1;
}
}
double a4=(double)A4.item/COUNT;
if(A1.used==1) cout<<A1.item<<" ";
else cout<<"N ";
if(A2.used==1) cout<<A2.item<<" ";
else cout<<"N ";
if(A3.used==1) cout<<A3.item<<" ";
else cout<<"N ";
if(A4.used==1) printf("%.1f ",a4);
else cout<<"N ";
if(A5.used==1) cout<<A5.item;
else cout<<"N";
}
简单题,无脑写