PAT 乙级 (Basic Level) Practice (中文)1012

1012 数字分类 (20 分)

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:

A​1= 能被 5 整除的数字中所有偶数的和;
A2= 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n1 −n2+n3​​ −n4⋯;
A3= 被 5 除后余 2 的数字的个数;
A4= 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;
A5= 被 5 除后余 4 的数字中最大数字。
输入格式:
每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。

输出格式:
对给定的 N 个正整数,按题目要求计算 A​1~A​5
​​ 并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出 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 <cstdio>
int main () {
	int ans [5] = {0} ;  	//ans存放输出数字 
	int count [5] = {0} ;	//count存放每种类型数字的个数 
	int N, temp;			//N为数字个数 temp为各个数字 
	scanf ("%d", &N) ;
	for (int i=0; i<N; i++) {
		scanf ("%d", &temp) ;
		if (temp%5 == 0) {				//A1 
			if (temp % 2 == 0) {
				ans [0] += temp;
				count [0] ++ ;
			}
		} else if (temp%5 == 1) {		//A2 
			count [1] ++ ;
			if (count [1] % 2 != 0) ans [1] += temp;
			else ans [1] -= temp; 
		} else if (temp % 5 == 2) {		//A3
			count [2] ++ ;
			ans [2] ++ ;
		} else if (temp % 5 == 3) {		//A4
			count [3] ++ ;
			ans [3] += temp ;
		} else if (temp % 5 == 4) {		//A5
			count [4] ++ ;
			if (temp > ans [4]) {
				ans [4] = temp ;
			}
		}
	}
	if (count [0] == 0) printf ("N ") ;
	else printf ("%d ", ans [0]) ;
	if (count [1] == 0) printf ("N ") ;
	else printf ("%d ", ans [1]) ;
	if (count [2] == 0) printf ("N ") ;
	else printf ("%d ", ans [2]) ;
	if (count [3] == 0) {
		printf ("N ") ;
	} else {
		printf ("%.1f ", (double)ans [3] / count [3]) ; // (double)ans 表示对该数字取double型 
	}
	if (count [4] == 0) {
		printf ("N") ;
	} else {
		printf ("%d", ans [4]) ;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值