1012 digital classification (20 points)
Given a series of positive integers, please classify the numbers as required and output the following 5 numbers:
- A 1 = the sum of all the even numbers in the number divisible by 5;
- A 2 = interleave and sum the remaining 1 numbers divided by 5 in the given order, i.e. calculate n 1 − N 2 + n 3 − n 4;
- A 3 = the number of the remaining 2 digits divided by 5;
- A 4 = the average of the numbers divided by 5 and remaining 3, accurate to 1 decimal place;
- A 5 = the largest of the remaining 4 digits divided by 5.
Input format:
Each input contains 1 test case. Each test case first gives a positive integer N of no more than 1000, and then gives N positive integers of no more than 1000 to be classified. Numbers are separated by spaces.
Output format:
For the given N positive integers, calculate A 1 ~ A 5 according to the requirements of the topic and output them in A row. Numbers are separated by spaces, but no extra spaces are allowed at the end of the line.
If one of the numbers does not exist, output N at the corresponding position.
Enter 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
Enter example 2:
8 1 2 4 5 6 7 9 16
Output example 2:
N 11 2 N 9
#include <stdio.h> #include <stdlib.h> int a1(int n) { if(n%10==0) return 1; else return 0; } int a2(int n) { if(n%5==1) return 1; else return 0; } int a3(int n) { if(n%5==2) return 1; else return 0; } int a4(int n) { if(n%5==3) return 1; else return 0; } int a5(int n) { if(n%5==4) return 1; else return 0; } int main() {int n,i,count,count1,count2,count3,count4,sum=0,sum1=0,sum2=0,max=0,flag=1; double average=0; int a[1005]; count=0,count1=0,count2=0,count3=0,count4=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { if(a1(a[i])==1) { count++; sum+=a[i]; } if(a2(a[i])==1) { count1++; sum1+=a[i]*flag; flag=-flag; } if(a3(a[i])==1) { count2++; } if(a4(a[i])==1) { count3++; sum2+=a[i]; average=sum2*1.0/count3; } if(a5(a[i])==1) { count4++; if(max<a[i]) max=a[i]; } } if(count==0) printf("N "); else printf("%d ",sum); if(count1==0) printf("N "); else printf("%d ",sum1); if(count2==0) printf("N "); else printf("%d ",count2); if(count3==0) printf("N "); else printf("%.1f ",average); if(count4==0) printf("N"); else printf("%d",max); return 0; }