【PAT】1012. Number Classification(20)

1012. Number Classification(20)

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

  • A1 = the sum of all even numbers in numbers divisible by 5;
  • A2 = Interleave the sum of the numbers that are divided by 5 and have 1 remaining in the given order, that is, calculate n1-n2+n3-n4...;
  • A3 = the number of digits with 2 remaining after dividing by 5;
  • A4 = the average of the numbers with the remainder 3 after dividing by 5, accurate to 1 decimal place;
  • A5 = The largest number of numbers that have a remainder of 4 when divided 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

Program Description:

  1. Use while(cin>>n) to input, press "ctrl"+"z" and then press Enter to stop input

  2. When calculating A2, the number of 1 remaining after the record is divided by 5 is n (the initial value is 0), and then the n-th power of (-1) is calculated to realize the interleaved summation, where pow(a,b) Function means to find a to the b power

else if(num[i]%5==1){
	mul=pow(-1,j);
	sum2+= num[i]*mul;
	j++;
}

  3. When finding A4, there are two ways to divide two integers to retain the fractional part:

int a,b;
float c;
c=(1.0*a)/(1.0*b);
c=(float)a/(float)b;

  4. To control the number of digits after the output decimal point, use the #include <iomanip> header file, which controls the format of input and output:

cout<<setiosflags(ios::fixed)<<setprecision(n)<<avr;
// n is the number of digits after the output decimal point, and avr is the floating point number to be output

  

The C++ code is as follows:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int max(int a,int b){
 5     if(a<b) return b;
 6     else return a;
 7 }
 8 int main() {
 9     vector<int> num;
10     int n,sum1,sum2,sum3,count,count1,count2,count3,count4,j=0,temp=0;
11     count=count1=count2=count3=count4=0;
12     sum1=sum2=sum3=0;
13     float avr;
14     int max_num,mul;
15     while(cin>>n){
16         num.push_back(n);
17     }
18     n=num[0];
19     for(int i=1;i<=n;i++){
20         if(num[i]%5==0){
21             if(num[i]%2==0){
22                 sum1+=num[i];
23                 count++;
24             }
25         }
26         else if(num[i]%5==1){
27             mul=pow(-1,j);
28             sum2+= num[i]*mul;
29             j++;
30         }
31         else if(num[i]%5==2){
32             count2++;
33         }
34         else if(num[i]%5==3){
35             sum3+=num[i];
36             count3++;
37         }
38         else{
39             max_num=max(temp,num[i]);
40             temp=max_num;
41             count4++;
42         }
43     }
44     if(count>0) cout<<sum1<<' ';
45     else cout<<"N ";
46     if(j>0) cout<<sum2<<' ';
47     else cout<<"N ";
48     if(count2>0) cout<<count2<<' ';
49     else cout<<"N ";
50     if(count3>0) {
51         avr =(1.0*sum3)/(1.0*count3);
52         cout<<setiosflags(ios::fixed)<<setprecision(1)<<avr<<' ';
53     }
54     else cout<<"N ";
55     if(count4>0) cout<<max_num;
56     else cout<<"N";
57     system("pause");
58     return 0;
59 }

 

Guess you like

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