牛客网_PAT乙级1002_数字分类 (20)

#include
#include
int main()
{
	int num;
	int a[1000] = { 0 };
	int i;
	int fuhao = 1;
	int temp3 = 0;
	int temp2;
	float temp4 = 0;
	int total_num4 = 0;
	int max5;
	int flag[6] = { 0 };//0表示没有
	int A1 = 0;//A1 = 能被5整除的数字中所有偶数的和;
	int A2 = 0;//A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;
	int A3 = 0;//A3 = 被5除后余2的数字的个数;
	int A4 = 0;//A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
	int A5 = 0;//A5 = 被5除后余4的数字中最大数字。
	scanf("%d", &num);
	for (i = 0; i < num; i++)
	{
		scanf("%d", &a[i]);
	}
	max5 = -10000;
	for (i = 0; i < num; i++)
	{
		if (a[i] % 5 == 0 && a[i] % 2 == 0)
		{
			flag[1] = 1;
			A1 = A1 + a[i];
			continue;
		}
		if (a[i] % 5 == 1)
		{
			flag[2] = 1;
			temp2 = fuhao * a[i];
			A2 = A2 + temp2;
			fuhao = -fuhao;
			continue;
		}
		if (a[i] % 5 == 2)
		{
			flag[3] = 1;
			temp3++;
			continue;
		}
		if (a[i] % 5 == 3)
		{
			flag[4] = 1;
			total_num4++;
			temp4 = temp4 + a[i];//这是总数,平均数还要除以total_num4++
			continue;
		}
		if (a[i] % 5 == 4)
		{

			if (a[i] > max5)
			{
				flag[5] = 1;
				max5 = a[i];
				continue;
			}
		}
	}


	if (flag[1] == 1)
	{
		printf("%d ", A1);
	}
	else
	{
		printf("N ");
	}
	if (flag[2] == 1)
	{
		printf("%d ", A2);
	}
	else
	{
		printf("N ");
	}
	if (flag[3] == 1)
	{
		printf("%d ", temp3);
	}
	else
	{
		printf("N ");
	}
	if (flag[4] == 1)
	{
		printf("%1.1f ", temp4 / total_num4);
	}
	else
	{
		printf("N ");
	}
	if (flag[5] == 1)
	{
		printf("%d", max5);
	}
	else
	{
		printf("N");
	}

}

你可能感兴趣的:(牛客网_PAT乙级1002_数字分类 (20))