CCF 201903-1 小中大(100分)

题库链接:https://blog.csdn.net/weixin_43905586/article/details/88404105

题滴链接:http://118.190.20.162/view.page?gpid=T89

问题描述

试题编号: 201903-1
试题名称: 小中大
时间限制: 1.0s
内存限制: 512.0MB
问题描述:


 

鄙人拙见:

  1. 分别找出最大最小值与中位数
  2. 注意:中位数可以为小数

 java参考代码1.0:

import java.util.Scanner;

public class Main 
{
	public static int Max(int []data,int count)
	{
		int max = -123456789;
		for(int i = 0; i < count; i++)
		{
			if(data[i] > max)
			{
				max = data[i];
			}
		}
		return max;
	}
	
	public static int Min(int []data,int count)
	{
		int min = 123456789;
		for(int i = 0; i < count; i++)
		{
			if(data[i] < min)
			{
				min = data[i];
			}
		}
		return min;
	}
	
	public static double Middle(int []data,int count)
	{
		double middle;
		if(count % 2 == 1)
		{
			middle = data[count / 2];
		}
		else
		{
			middle = (double)( data[count / 2] + data[count / 2 - 1] ) / 2;
		}
		
		return middle;
	}
	
	public static void main(String [] args)
	{
		Scanner input = new Scanner(System.in);
		int count = input.nextInt();
		int []data = new int[count];
		for(int i = 0;i < count; i++)
		{
			data[i] = input.nextInt();
		}
		int max = Max(data,count);
		int min = Min(data,count);
		double middle = Middle(data,count);
		
		System.out.print(max+" ");
		
		if((int)middle == middle)
		{
			System.out.print((int)middle+" ");
		}
		else
		{
			System.out.print(middle+" ");
		}

		System.out.print(min);
		
	}
	
	
}

 

你可能感兴趣的:(每日一练)