# 算法学习之路|数字分类

A1 = 能被5整除的数字中所有偶数的和；A2 = 将被5除后余1的数字按给出顺序进行交错求和，即计算n1-n2+n3-n4...；A3 = 被5除后余2的数字的个数；A4 = 被5除后余3的数字的平均数，精确到小数点后1位；A5 = 被5除后余4的数字中最大数字。

13 1 2 3 4 5 6 7 8 9 10 20 16 18

30 11 2 9.7 9

8 1 2 4 5 6 7 9 16

N 11 2 N 9

#include<iostream>
#include<vector>
#include<stdio.h>
using namespace std;

void A1(vector<int> v){
int sum=0;
for(int i=0;i<v.size();i++){
if(v[i]%5==0&&v[i]%2==0)
sum+=v[i];
}
if(sum==0)
cout<<"N"<<" ";
else
cout<<sum<<" ";
}
void A2(vector<int> v){
int ch=1;
int sum=0;
int flag=0;
for(int i=0;i<v.size();i++){
if(v[i]%5==1){
sum+=(v[i]*ch);
ch*=-1;
flag++;
}
}
if(flag==0)
cout<<"N"<<" ";
else
cout<<sum<<" ";
}
void A3(vector<int> v){
int count=0;
for(int i=0;i<v.size();i++){
if(v[i]%5==2)
count++;
}
if(count==0)
cout<<"N"<<" ";
else
cout<<count<<" ";
}
void A4(vector<int> v){
double avg=0;
int count=0;
for(int i=0;i<v.size();i++){
if(v[i]%5==3){
count++;
avg+=v[i];
}
}
avg/=count;
if(count==0)
cout<<"N"<<" ";
else
printf("%.1f ",avg);
}
void A5(vector<int> v){
int max=0;
for(int i=0;i<v.size();i++){
if(v[i]%5==4)
if(v[i]>max)
max=v[i];
}
if(max==0)
cout<<"N";
else
cout<<max;
}

int main(){
int n,a;
cin>>n;
vector<int>v;
while(n--){
cin>>a;
v.push_back(a);
}
A1(v);
A2(v);
A3(v);
A4(v);
A5(v);
}

+ 订阅