PAT 1012. Classification of Numbers

Given a series of positive integers, sort the numbers as required and output the following 5 numbers:

A1 = sum of all even numbers in numbers divisible by 5; A2 = interleaved summation of numbers with remainder 1 after division by 5 in the given order, that is, calculate n1-n2+n3-n4...; A3 = be The number of digits with a remainder of 2 after division by 5; A4 = the average number of digits with a remainder of 3 after division by 5, accurate to 1 decimal place; A5 = the largest number of digits with a remainder of 4 after division by 5.

Input format:

Each input contains 1 test case. Each test case first gives a positive integer N not exceeding 1000, and then gives N positive integers not exceeding 1000 to be classified. The numbers are separated by spaces.

Output format:

For the given N positive integers, calculate A1~A5 according to the requirements of the question and output them sequentially in one line. Numbers are separated by spaces, but there must be no extra spaces at the end of the line.

If one of the types of numbers does not exist, output "N" in the corresponding position.

Input sample 1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
Sample output 1:
30 11 2 9.7 9
Input sample 2:
8 1 2 4 5 6 7 9 16
Sample output 2:
N 11 2 N 9
#include<stdio.h>
intmain()
{
	int n,n1,s[1001];
	int num=0,num1=0,num2=0,num3=0,sum=0,sum1=0,ss[1001];
	double sum2=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&n1);
		s[i]=n1;
	}
	int max=0;
	for(int j=0;j<n;j++)
	{
		if(s[j]%5==0)
		{
			if(s[j]%2==0)
			{
				sum=sum+s[j];
				num++;
			}
		}	
	}
	if(num!=0)
	printf("%d ",sum);
	else
	printf("N ");
	for(int j=0;j<n;j++)
	{
		if(s[j]%5==1)
		{
			ss[num1++]=s[j];
		}
	}
	if(num1!=0)
	{
		for(int j=0;j<num1;j++)
		{
			if(j%2==0)
			sum1=sum1+ss[j];
			else
			sum1=sum1-ss[j];
		}
		printf("%d ",sum1);
	}
	else
	printf("N ");
	for(int j=0;j<n;j++)
	{
		if(s[j]%5==2)
		{
			num2++;
		}
	}
	if(num2!=0)
	printf("%d ",num2);
	else
	printf("N ");
	for(int j=0;j<n;j++)
	{
		if(s[j]%5==3)
		{
			sum2=sum2+s[j];
			num3 ++;
		}
	}
	if(sum2!=0)
	printf("%.1f ",sum2/num3);
	else
	printf("N ");
	for(int j=0;j<n;j++)
	{
		if(s[j]%5==4)
		{
			if(s[j]>max)
			{
				max=s[j];
			}
		}
	}
	if(max!=0)
	printf("%d\n",max);
	else
	printf("N\n");
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325706415&siteId=291194637