CCF CSP Brush Question Record 29-201903-1 Small, Medium and Large (Java)

 

At first, this question was 80 points, and then I thought that it was a wrong question. It was rounded to one decimal place. I made it to preserve the whole number. There is also a pitfall to remember to judge whether mid is an integer. The following is the 100 points code:

import java.util.Scanner;
public class 小中大 {
	public static void main(String args[]){
	Scanner sc=new Scanner(System.in);
	int n=sc.nextInt();
	long[] a=new long[n];
	for(int i=0;i<n;i++){
		a[i]=sc.nextInt();
	}
	long max=a[0];
	long min=a[0];
	for(int i=1;i<n;i++){
		if(a[i]>max){
			max=a[i];
		}
		if(min>a[i]){
			min=a[i];
		}
	}
	double mid=0.0;
	if((n-1)%2==0){
		mid=a[(n-1)/2];
	}else{
		double s=0.0;
		s=(a[(n-1)/2]+a[(n-1)/2+1]);
		mid=s/2;
	}
	if(mid-(int)mid==0){
		System.out.println(max+" "+(int)mid+" "+min);
	}else{
		System.out.println(max+" "+mid+" "+min);
	}

}
	
}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108405318