当前位置:网站首页>Pat class B 1012 C language

Pat class B 1012 C language

2022-06-23 05:54:00 Octopus bro

1012. Digital classification (20)


Given a series of positive integers , Please classify the numbers as required , And output the following 5 A digital :

  • A1 = Can be 5 The sum of all even numbers in a divisible number ;
  • A2 = Will be 5 Except after 1 The numbers of are interleaved and summed in the given order , Computation n1-n2+n3-n4...;
  • A3 = By 5 Except after 2 Number of digits of ;
  • A4 = By 5 Except after 3 The average number of , Accurate to the decimal point 1 position ;
  • A5 = By 5 Except after 4 The largest number of .

    Input format :

    Each input contains 1 Test cases . Each test case shall be provided with no more than one 1000 The positive integer N, Subsequently given N No more than one. 1000 Of positive integers to be classified . Numbers are separated by spaces .

    Output format :

    For given N A positive integer , Calculated according to the requirements of the title A1~A5 And output in order in one line . Numbers are separated by spaces , But there must be no extra space at the end of the line .

    If one of the numbers does not exist , Output at corresponding position “N”.

    sample input 1:
    13 1 2 3 4 5 6 7 8 9 10 20 16 18
    
    sample output 1:
    30 11 2 9.7 9
    
    sample input 2:
    8 1 2 4 5 6 7 9 16
    
    sample output 2:
    N 11 2 N 9
    


Code :

// Starting time 20:49 
// End time 21:40 
#include "stdio.h"
#include "stdlib.h"
int main()
{
	int N;
	int A1,A2,A3,A5;
	A1 = A2 = A3 = A5 = 0;
	int flag_2 = -1;//2 The sign bit of  
	double A4;//A4 To keep the decimal point 
	A4 = 0.0;
	int count_1,count_2,count_3,count_4,count_5;
	count_1 = count_2 = count_3 = count_4 = count_5 = 0;
	scanf("%d",&N);
	int * array = (int *)malloc(N * sizeof(int));
	int i;
	for(i = 0; i < N; i++)
	{
		scanf("%d",&array[i]);
	}
	for(i = 0; i < N; i++)
	{
		switch(array[i] % 5)
		{
			case 0:	
				if(array[i] % 2 == 0)
				{
					A1 += array[i];
					count_1++; 
				}				
				break;
			case 1: 
				flag_2 *= -1;
				A2 += flag_2*array[i];
				count_2++;
				break;
			case 2:
				count_3++;
				break;
			case 3:
				count_4++;
				A4 += array[i]; 
				break;
			case 4:
				if(A5 < array[i])
					A5 = array[i];
				count_5++;
				break;
			default: break;								
		}
	}

       A4 = A4 / count_4;
	if(count_1 == 0)
		printf("N ");
	else 
		printf("%d ",A1);
	if(count_2 == 0)
		printf("N ");
	else 
		printf("%d ",A2);
	if(count_3 == 0)
		printf("N ");
	else 
		printf("%d ",count_3);
	if(count_4 == 0)
		printf("N ");
	else 
		printf("%.1f ",A4);
	if(count_5 == 0)
		printf("N");
	else 
		printf("%d",A5);
	
	return 0;
}



原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410348310.html