class=”markdown_views prism-atom-one-dark”>
Overview of the topic:
Given a series of positive integers, please classify the numbers as required, and output the following 5 numbers:
A1 = Among the numbers divisible by 5 The sum of all even numbers;
A2 = The numbers with a remainder of 1 after division by 5 are interleaved and summed in the given order, that is, calculating n1-n2+n3-n4…;
A3 = The remainder of 2 after division by 5 The number of digits;
A4 = the average number of numbers with a remainder of 3 after division by 5, accurate to 1 decimal place;
A5 = the largest number of numbers 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. Numbers are separated by spaces.
Output format:
For the given N positive integers, calculate A1~A5 according to the title 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 numbers does not exist, then output “N” in the corresponding position.
Input example 1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
Output example 1:
30 11 2 9.7 9
Input example 2:
8 1 2 4 5 6 7 9 16
Output example 2:
N 11 2 N 9
Thoughts:
Enter the value and deal with it according to the conditions
#include
int flag = 0;
int casenum;
int N;
int num = 1;
int A1sum=0, A2sum = 0 , A3num = 0,A5max=-1;
int A4num = 0;
float A4avg = 0.0;
int a1count = 0, a2count = 0 , a3count = 0, a4count = 0, a5count = 0 ;
int main() {
scanf("%d", &casenum);
for (int i = 0; i < casenum; i++)
{
scanf("%d", &N);
int n = N % 5; //According to different remainders Process
switch (n)
{
case 0:
{
if (!(N % 2))
{
A1sum += N;
a1count++;
}
break;
}
case 1:
{
if (!flag)
{
a2count++;
A2sum += N;
flag = 1;
}
else
{
a2count++;
A2sum -= N;
flag = 0;
}
break;
}
case 2:
{
A3num++;
a3count++;
break;
}
case 3:
{
A4avg = A4avg + N;
a4count++;
A4num++;
break;
}
case 4:
{
if (N > A5max)
A5max = N;
a5count++;
break;
}
}
}
if (a1count) printf("%d ", A1sum);
else printf("N ");
if (a2count) printf("%d ", A2sum);
else printf("N ");
if (a3count) printf("%d ", A3num);
else printf("N ");
if (a4count) printf("%.1lf ", A4avg / A4num);
else printf("N ");
if (a5count) printf("%d", A5max);
else printf("N");
return 0;
}