CodeUp-Question G: Detailed problem solving ideas for number classification

题目描述
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:

A1 = 能被5整除的数字中所有偶数的和;
A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...
A3 =5除后余2的数字的个数;
A4 =5除后余3的数字的平均数,精确到小数点后1位;
A5 =5除后余4的数字中最大数字。
输入
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。
输出
对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出“N”。

样例输入 Copy
13 1 2 3 4 5 6 7 8 9 10 20 16 18
8 1 2 4 5 6 7 9 16
样例输出 Copy
30 11 2 9.7 9
N 11 2 N 9
  1. The question requires to
    enter an N, and then enter N numbers in the same line, if these numbers meet A1~A5, then output the result, otherwise output N

  2. Main idea
    Every time a number is input, it is added to judge the result.
    Use f1~f5 to identify whether there are five types of numbers
    A1:

    	if(x%5==0&&x%2==0){
          
          
    				f1=1;
    				a1+=x;
    			}
    

    A2: Pay attention to the positive and negative interleaving, just multiply by -1 each time

    if(x%5==1){
          
          
    				f2=1;
    				a2+=x*temp;
    				temp*=-1;
    			}
    

    A3 :

    	if(x%5==2){
          
          
    				f3=1;
    				a3++;
    			} 
    

    A4: Note that it is troublesome to calculate the average, loop accumulation is more troublesome, but it can be calculated outside the loop, and the sum is directly divided by the number; also note that the result is a floating point number, so it is defined as a double variable

    	if(x%5==3){
          
          
    				f4++;
    				a4+=x;
    			} 
    
    if(f4!=0)
    			printf("%.1lf ",a4/f4);
    		else
    			cout<<"N"<<" ";
    

    A5 :

    if(x%5==4){
          
          
    				f5=1;
    				if(x>a5)
    				a5=x;
    			} 
    
  3. Complete code

    #include<iostream>
    #include<stdio.h>
    using namespace std;
    int main(){
          
          
    	int n,x;
    	int i,j;
    	int a1,a2,a3,a5;
    	double a4;//计算A1,A2,A3,A4,A5 
    	int f1,f2,f3,f4,f5;//标识是否存在 
    	while(cin>>n){
          
          
    		int temp=1;//A2交错级数
    		f1=f2=f3=f4=f5=0;
    		//判断A1是否存在 
    		int a1=a2=a3=a4=a5=0;
    		for(j=0;j<n;j++){
          
          
    			cin>>x;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    			if(x%5==0&&x%2==0){
          
          
    				f1=1;
    				a1+=x;
    			}
    			
    		//判断A2是否存在
    			if(x%5==1){
          
          
    				f2=1;
    				a2+=x*temp;
    				temp*=-1;
    			}	
    			
    		//判断A3是否存在
    			if(x%5==2){
          
          
    				f3=1;
    				a3++;
    			} 
    		//判断A4是否存在
    			if(x%5==3){
          
          
    				f4++;
    				a4+=x;
    			} 
    		//判断A5是否存在
    			if(x%5==4){
          
          
    				f5=1;
    				if(x>a5)
    				a5=x;
    			} 
    		}
    		if(f1!=0)
    			cout<<a1<<" ";
    		else
    			cout<<"N"<<" ";
    		if(f2!=0)
    			cout<<a2<<" ";
    		else
    			cout<<"N"<<" ";	
    		if(f3!=0)
    			cout<<a3<<" ";
    		else
    			cout<<"N"<<" ";	
    		if(f4!=0)
    			printf("%.1lf ",a4/f4*1.0);
    		else
    			cout<<"N"<<" ";
    		if(f5!=0)
    			cout<<a5<<endl;
    		else
    			cout<<"N"<<endl;	
    	}
    	return 0;
    } 
    

Guess you like

Origin blog.csdn.net/weixin_44549439/article/details/112706881