首页 > 专题系列 > Java解POJ > POJ 2833 The Average [解题报告] Java
2013
11-12

POJ 2833 The Average [解题报告] Java

The Average

问题描述 :

In a speech contest, when a contestant finishes his speech, the judges will then grade his performance. The staff remove the highest grade and the lowest grade and compute the average of the rest as the contestant’s final grade. This is an easy problem because usually there are only several judges.

Let’s consider a generalized form of the problem above. Given n positive integers, remove the greatest n1 ones and the least n2 ones, and compute the average of the rest.

输入:

The input consists of several test cases. Each test case consists two lines. The first line contains three integers n1, n2 and n (1 ≤ n1, n2 ≤ 10, n1 + n2 < n ≤ 5,000,000) separate by a single space. The second line contains n positive integers ai (1 ≤ ai ≤ 108 for all i s.t. 1 ≤ in) separated by a single space. The last test case is followed by three zeroes.

输出:

For each test case, output the average rounded to six digits after decimal point in a separate line.

样例输入:

1 2 5
1 2 3 4 5
4 2 10
2121187 902 485 531 843 582 652 926 220 155
0 0 0

样例输出:

3.500000
562.500000

温馨提示:

This problem has very large input data. scanf and printf are recommended for C++ I/O.

The memory limit might not allow you to store everything in the memory.

解题代码:

//* @author: [email protected]
import java.io.*;
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  InputStreamReader is=new InputStreamReader(System.in);
  BufferedReader in=new BufferedReader(is);
  StringBuffer buf=new StringBuffer();
  try {
	while(true)
	{
         char c;
	  int[] u=new int[3];
	  for(int i=0;i< 3;i++)
	  {
	    while(true)
	    {
		c=(char)in.read();
		if(c==32||c==13)
			break;
		buf.append(c);
	     }
	     u[i]=Integer.parseInt(buf.toString());
	     buf.delete(0,buf.length());
	   }
	  if(u[0]==0&&u[1]==0&&u[2]==0)break;
	  int[] max=new int[u[0]];
	  int[] min=new int[u[1]];
	  for(int i=0;i< u[1];i++)
		min[i]=9999999;
	  double total=0;
	  in.read();
	  for(int i=0;i< u[2];i++)
	  {
	   while(true)
	   {
		c=(char)in.read();
		if(c==32||c==13)
			break;
		buf.append(c);
	    }
	   int w=Integer.parseInt(buf.toString());
	   buf.delete(0,buf.length());
	   if(max[0]< w)max[0]=w;
	   if(min[u[1]-1]>w)min[u[1]-1]=w;
	   Arrays.sort(max);
	   Arrays.sort(min);
	   total+=w;
	  }
	 for(Integer e:max)
		total-=e;
	for(Integer e:min)
		total-=e;
	in.read();
	System.out.printf("%.6f\n",total/(u[2]-u[1]-u[0]));
   }
  } catch (IOException e) {}
 }
}

  1. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。