1. 程式人生 > >第六屆山東省ACM競賽 A題 Nias and Tug-of-War(java)

第六屆山東省ACM競賽 A題 Nias and Tug-of-War(java)

Nias and Tug-of-War
Time Limit: 1000MS Memory limit: 65536K
題目描述
Nias is fond of tug-of-war. One day, he organized a tug-of-war game and invited a group of friends to take part in.
Nias will divide them into two groups. The strategy is simple, sorting them into a row according to their height from short to tall, then let them say one and two alternately (i.e. one, two, one, two…). The people who say one are the members of the red team while others are the members of the blue team.
We know that the team which has a larger sum of weight has advantages in the tug-of-war. Now give you the guys’ heights and weights, please tell us which team has advantages.

輸入
The first line of input contains an integer T, indicating the number of test cases.
The first line of each test case contains an integer N (N is even and 6 ≤ N ≤ 100).
Each of the next N lines contains two real numbers X and Y, representing the height and weight of a friend respectively.

輸出
One line for each test case. If the red team is more likely to win, output “red”, if the blue team is more likely to win, output “blue”. If both teams have the same weight, output “fair”.
示例輸入

1
6
170 55
165.3 52.5
180.2 60.3
173.3 62.3
175 57
162.2 50

示例輸出

blue

程式碼為:

import java.util.*;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

 class a{
    double h,t;
}

 class mycmp implements Comparator<a>{
     public int compare(a x,  a y)
     {
         if
(x.h!=y.h) { if(x.h-y.h<0) { return -1; }else { return 1; } } return 0; } } public class jisuan { public static void main(String[] args) { Scanner cin=new Scanner(System.in); int n=cin.nextInt(); a []qq=new a[100]; while(n--!=0){ int m=cin.nextInt(); int i; for(i=0;i<m;i++){ qq[i]=new a(); qq[i].h=cin.nextDouble(); qq[i].t=cin.nextDouble(); } Arrays.sort(qq, 0, m,new mycmp()); double t1=0,t2=0; for( i=0;i<m;i++) { if(i%2==0) { t1+=qq[i].t; } if(i%2==1) { t2+=qq[i].t; } } System.out.println(t1+" "+t2); } } }