数字分类 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)

题目描述

给定一系列正整数,请按要求对数字进行分类,并输出以下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>

#include<cmath>
#include<iomanip>
#include<cstdio>
using namespace std;
int A1(int n)
{
static int sum=0,k=0;
if(n%5==0&&n%2==0)
{sum+=n;
k++;}
if(k==0)
return -1;
else
return sum;
}
int A2(int n)
{
static int sum=0;
static int i=0;
if(n%5==1)
{
sum+=pow(-1,i)*n;
++i;
}
if(i==0)
return -1;
else
return sum;
}
int A3(int n)
{
static int k=0;
if(n%5==2)
++k;
if(k==0)
return -1;
else
return k;
}
float A4(int n)
{
static float sum=0;
static int k=0;
float ave;
if(n%5==3)
{
sum+=n;
++k;
}
ave=sum/k;
if(k==0)
return -1;
else
return ave;


}
int A5(int n)
{
static int max=-1;
if(n%5==4&&n>max)
max=n;
if(max==-1)
return -1;
else
return max;
}
int main()
{
int N,i,k=0,a1,a2,a3,a5;
float a4;
cin>>N;
while(1)
{cin>>i;
k++;
a1=A1(i);
a2=A2(i);
a3=A3(i);
a4=A4(i);
a5=A5(i);
if(k==N)
break;}
if(a1==-1)
cout<<"N ";
else
cout<<a1<<" ";
if(a2==-1)
cout<<"N ";
else
cout<<a2<<" ";
if(a3==-1)
cout<<"N ";
else
cout<<a3<<" ";
if(a4==-1)
cout<<"N ";
else
printf("%.1f ",a4);
if(a5==-1)
cout<<"N"<<endl;
else
cout<<a5<<endl;
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42433442/article/details/80835369