PAT(B)数字分类 (20)

 

题目链接:https://www.nowcoder.com/pat/6/problem/4078

https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840

1012 数字分类 (20)(20 分)

给定一系列正整数,请按要求对数字进行分类,并输出以下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;
int n;
int a[1005];
int ans1,ans2,ans3,ans5;
double ans4;
int main(){
    cin>>n;
    int flag=1;
	int cnt=0;
	int cnt1=0;
	int ans5=0;
    for(int i=0;i<n;i++){
    	cin>>a[i];
    	if(a[i]%5==0&&a[i]%2==0){
    		ans1+=a[i];
		}else if(a[i]%5==1){
			ans2+=flag*a[i];
			flag*=-1;
			cnt1++;
		}else if(a[i]%5==2){
			ans3++;
		}else if(a[i]%5==3){
			ans4+=a[i];
			cnt++;
		}else if(a[i]%5==4){
			if(a[i]>ans5){
				ans5=a[i];
			}
		}
	}
	if(!ans1){
		printf("N ");
	}else{
		printf("%d ",ans1);
	}
	if(!cnt1){
		printf("N ");
	}else{
		printf("%d ",ans2);
	}
	if(!ans3){
		printf("N ");
	}else{
		printf("%d ",ans3);
	}
	if(!ans4){
		printf("N ");
	}else{
		printf("%.1f ",ans4/cnt);
	}
	if(!ans5){
		printf("N");
	}else{
		printf("%d",ans5);
	}
	//printf("%d %d %d %.1lf %d\n",ans1,ans2,ans3,ans4/cnt,ans5);
	return 0;
}

 

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	int n;
	int a[1005];
	int b[6];//记录5类操作的结果 
	int c[6];//记录5类数字是否存在 
	while(cin>>n){
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		double avg=0;
		int f=1;
		for(int i=0;i<n;i++){
			cin>>a[i];
			int mod=a[i]%5;
			switch(mod){
				case 0:
					if(a[i]%2==0){
						c[1]++;
						b[1]+=a[i];
					}
					break;	
				case 1:
					c[2]++;
					b[2]+=f*a[i];
					f*=-1;
					break;	
				case 2:
					c[3]++;
					b[3]++;
					break;
				case 3:
					c[4]++;
					avg+=a[i];
					b[4]++;
					break;
				case 4:
					c[5]++;
					if(a[i]>b[5]){
						b[5]=a[i];
					}
					break;
			}
		}
		for(int i=1;i<=3;i++){
			if(c[i]){
				cout<<b[i]<<" ";
			}else{
				cout<<"N ";
			}
			
		}
		if(c[4]){
			printf("%.1f ",avg/b[4]);
		}else{
			printf("N ");
		}
		if(c[5]){
			printf("%d\n",b[5]);
		}else{
			printf("N\n");
		}
		 	
	}	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值