【九度OJ】1007【计算名次】

采用数据结构书上计算名次的算法,复杂度n*(n-1)/2

代码:

package Test1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Test32_1007 {

	/**
	 * by qr jobdu 1007   2014-8-18
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//1.只排那m个国家    2.注意精度,用double
		StreamTokenizer st = new StreamTokenizer(new BufferedReader(
				new InputStreamReader(System.in)));
		
		int n,m;
		while(st.nextToken()!=StreamTokenizer.TT_EOF){
			n=(int)st.nval;
			
			st.nextToken();
			m=(int)st.nval;
			
			double goldmedal[]=new double[n];  //rank1
			double medal[]=new double[n];      //rank2
			int people[]=new int[n];
			
			double goldmper[]=new double[n];  //rank3
			double medalper[]=new double[n];  //rank4
			
			int rank[][]=new int[4][n];
			
			for(int i=0;i<n;i++){
				st.nextToken();
				goldmedal[i]=st.nval;
				
				st.nextToken();
				medal[i]=st.nval;
				
				st.nextToken();
				people[i]=(int)st.nval;
				
				goldmper[i]=goldmedal[i]/people[i];
				medalper[i]=medal[i]/people[i];
				
				for(int j=0;j<4;j++)
					rank[j][i]=1;
				
			}
			
			int flag[]=new int[n];  //0代表这个国家没有被选上  1代表被选上了
			int sortcountry[]=new int[m];
			int temp;
			for(int i=0;i<m;i++){
				st.nextToken();
				temp=(int)st.nval;
				
				sortcountry[i]=temp;
				flag[temp]=1;
			}
			
			//compute the rank
			ComputeRank(goldmedal,rank[0],n,flag);
			ComputeRank(medal,rank[1],n,flag);
			ComputeRank(goldmper,rank[2],n,flag);
			ComputeRank(medalper,rank[3],n,flag);

			for(int i=0;i<m;i++){
				temp=sortcountry[i];
				
				int min=n+1;
				int type=0;
				for(int j=0;j<4;j++){
					if(rank[j][temp]<min){
						min=rank[j][temp];
						type=j+1;
					}
				}
				
				System.out.println(min+":"+type);
			}
			
			System.out.println();
		}
		
	}

	private static void ComputeRank(double[] arr, int[] rank, int n, int[] flag) {  //名次从0开始,并列的名次相同
		for(int i=0;i<n;i++){
			if(flag[i]==0)
				continue;
			for(int j=0;j<i;j++){
				if(flag[j]==0)
					continue;
				if(arr[i]>arr[j]){  //谁小谁的名次就加1
					rank[j]++;
				}else if(arr[i]<arr[j]){
					rank[i]++;
				}
			}	
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值