其他分享
首页 > 其他分享> > PAT (Basic Level) Practice 1012 数字分类

PAT (Basic Level) Practice 1012 数字分类

作者:互联网

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

输入格式:

每个输入包含 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<iostream>
#include<iomanip>
using namespace std;

int main()
{
	int n, i;
	int a[1001];
	cin >> n;
	for (i = 0; i < n; i++)
		cin >> a[i];

	int a1 = 0, a2 = 0, a3 = 0, a5 = 0;
	int a11 = 0, a22 = 0, a33 = 0, a44 = 0, a55 = 0;
	double a4 = 0;
	int q = -1;
	for (i = 0; i < n; i++)
	{
		if (a[i] % 10 == 0)
		{
			a1 += a[i];
			a11++;
		}
		if (a[i] % 5 == 1)
		{
			q = -q;
			a2 += a[i] * q;
			a22++;
		}
		if (a[i] % 5 == 2)
		{
			a3++;
			a33++;
		}
		if (a[i] % 5 == 3)
		{
			a4 += a[i];
			a44++;
		}
		if (a[i] % 5 == 4)
		{
			if (a[i] > a5)
				a5 = a[i];
			a55++;
		}
	}
	
	if (a11 != 0)
		cout << a1<<" ";
	else
		cout << "N"<<" ";
	if (a22 != 0)
		cout << a2 << " ";
	else
		cout << "N" << " ";
	if (a33 != 0)
		cout << a3 << " ";
	else
		cout << "N" << " ";
	if (a44 != 0)
		printf("%.1f ", a4 / a44);
		//cout << setprecision(2) << a4 / a44 << " ";
	else
		cout << "N" << " ";
	if (a55 != 0)
		cout << a5  ;
	else
		cout << "N" ;
	return 0;
}

 

标签:除后,输出,PAT,数字,Level,++,Practice,样例,int
来源: https://blog.csdn.net/qq_38344751/article/details/90342414