输入样例 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
题目分析
知识点:
- c++的保留一位小数: cout<<fixed<<setprecision(1)<<A4<<’ ';
定义于头文件 < iomanip>
C++标准库中的iomanip- 用count1 count2 count3 count4 coun5 是否大于0 判断是否有数,是否输出’N’
- A2的时候可以用 sum=sum+pow(-1,count2)*b[count2];
- #include < algorithm>算法函数
- algorithm头文件常用函数
-借鉴学习链接-
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n; cin>>n;
int arr[10000];
for(int i=0;i<n;i++){
cin>>arr[i];
}
//求A1=sum1
int sum1=0,count1=0;
for(int j=0;j<n;j++){
if(arr[j]%5==0&&arr[j]%2==0){
sum1+=arr[j];
count1++;
}
}
if(count1>0){
cout<<sum1<<' ';
}else{
cout<<"N"<<' ';
}
//A2
int sum2=0,flag=1,count2=0;
for(int j=0;j<n;j++){
if(arr[j]%5==1){
count2++;
sum2+=(flag*arr[j]);
flag=-flag;
}
}
if(count2>0){
cout<<sum2<<' ';
}else{
cout<<"N"<<' ';
}
//A3
int count3=0;
for(int j=0;j<n;j++){
if(arr[j]%5==2){
count3++;
}
}
if(count3>0)
cout<<count3<<" ";
else
cout<<"N"<<" ";
//A4
double sum4=0;
int count4=0;
for(int j=0;j<n;j++){
if(arr[j]%5==3){
sum4+=arr[j];//求和
count4++;
}
}
double A4=sum4/count4;//求平均数
if(count4>0){ //注意
cout<<fixed<<setprecision(1)<<A4<<' ';
}else{
cout<<"N"<<" ";
}
//A5
int max=0;
for(int j=0;j<n;j++){
if(arr[j]%5==4&&arr[j]>max){
max=arr[j];
}
}
if(max==0){
cout<<"N";
}else{
cout<<max;
}
}