Pat Advanced Level 1025(Java and C++)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Java :</span>


import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Vector;

public class Main {
	public static void main(String[] arg) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.valueOf(sc.nextLine().trim());

		Vector<String[]> vc = new Vector<String[]>();
		String[] testee0;
		int[] allScores =new int[1];
		int k=0;
		for (int location = 1; location <= n; location++) {
			int num = Integer.valueOf(sc.nextLine().trim());
			int[] scoresPerGroup  =new int[num];
			int[] temp  =new int[num+k];
			for(int m =0;m<allScores.length;m++){
				temp[m] =allScores[m];
			}
			allScores =temp;
			for (int i = 0; i <num; i++) {
				String testee = sc.nextLine().trim();
				testee0 = new String[6];
				testee0[0] = testee.split(" ")[0];// registration_number
				testee0[1] = "";                  // final_rank
				testee0[2] = "" + location;       // location_number
				testee0[3] = "";                  // local_rank
				
				testee0[4] = testee.split(" ")[1];
				vc.add(testee0);
				scoresPerGroup[i]=Integer.valueOf(testee.split(" ")[1]);
				allScores[k]=Integer.valueOf(testee.split(" ")[1]);
				k++;
			}
			Arrays.sort(scoresPerGroup);
			int pre =-1;
			int[] mark =new int[k];
			
			//给每一组排序local rank
			for(int i=num-1 ;i>=0 ;i--){
				for(int j=k-num;j<k;j++){
					int sco =Integer.valueOf(vc.get(j)[4]);
					if(i==num-1 ){
						if(mark[j]!=1 && sco ==scoresPerGroup[i]){
						 vc.get(j)[3]=""+(num-i);
						 pre    = num-i;
						 mark[j]= 1;
						 break;
						}
					}
					else {
						if(mark[j]!=1 &&  scoresPerGroup[i]!=scoresPerGroup[i+1]  && sco ==scoresPerGroup[i]){
							vc.get(j)[3]=""+(num-i);
							pre    = num-i;
							mark[j]= 1;
							break;
						}
						else if(mark[j]!=1 &&  scoresPerGroup[i]==scoresPerGroup[i+1]  && sco ==scoresPerGroup[i]){
							vc.get(j)[3]=""+pre;
							mark[j]= 1;
						}
					}
				}
			}
		}
		
		
		//给所有Testee 做总排序 final_rank
		 Arrays.sort(allScores);
		 int[] mark2 =new int[k];
		 int   pre2 =-1;
		 int sco2  ;
		for(int i=k-1 ;i>=0;i--){
			for(int j=0;j<k;j++){
				sco2 =Integer.valueOf(vc.get(j)[4]);
				if(i==k-1 ){
					//第一个,单独作为一种情况,因为下面的[i+1],会outOfBounds
					if(mark2[j]!=1 && sco2==allScores[i]){
						vc.get(j)[1] =""+(k-i);
						pre2 =k-i;
						mark2[j] =1;
						vc.get(j)[5] =""+(k-i);
						break;
					}
				}
				else{
					if(mark2[j]!=1 && allScores[i]!=allScores[i+1] && sco2==allScores[i]){
						//与上一个分数不同,pre2更新,排名=k-i
						vc.get(j)[1] =""+(k-i);
						pre2 =k-i;
						mark2[j] =1;
						vc.get(j)[5] =""+(k-i);
						break;
					}
					else if(mark2[j]!=1 && allScores[i]==allScores[i+1] && sco2==allScores[i]){
						//与上一个分数相同,pre2不变,排名=pre2
						vc.get(j)[1] =""+pre2;
						mark2[j]=1;
						vc.get(j)[5] =""+(k-i);
					}
				}
			}
		}
		
		//将原来无序的Vector vc转化为有序的Vector vc_result(按输出先后的绝对顺序)
		String[] testee ;
		Vector<String[]> vc_result = new Vector<String[]>();
		for(int i=0;i<k;i++){
			for(int j=0;j<k;j++){
				testee=vc.get(j);
				if(Integer.valueOf(testee[5])==i+1){
					vc_result.add(testee);
				}
			}
		}
		
		TreeMap<String,String[]>  map_per_finalRank =new TreeMap<String,String[]>();
		
		
		String markfRank ="";
		
		System.out.println(k);
		
		//利用TreeMap(key自动排序,将RegisterId作为Key存入)
		//对同一个finalRank范围内的RegisterId进行排序,并输出。
        for(int i=0;i<k;i++){
        	testee =vc_result.get(i);
            if(map_per_finalRank.isEmpty()){//empty
            	markfRank  = testee[1];
             }
            else{// not empty
                if(testee[1].equals(markfRank)){//属于与上一个相同的markfRank
                }
                else{//属于新的一个markfRank
                	markfRank = testee[1];
                	Iterator<String> it =map_per_finalRank.keySet().iterator();
                	while(it.hasNext()){
                		String[] testee_out =map_per_finalRank.get(it.next());
              			System.out.print(testee_out[0]+" ");
            			System.out.print(testee_out[1]+" ");
            			System.out.print(testee_out[2]+" ");
            			System.out.println(testee_out[3]);
                	   }
                	 map_per_finalRank.clear();
                }
            }
            map_per_finalRank.put(testee[0], testee);
        }
        
        if(!map_per_finalRank.isEmpty()){
        	Iterator<String> it =map_per_finalRank.keySet().iterator();
        	while(it.hasNext()){
        		String[] testee_out =map_per_finalRank.get(it.next());
      			System.out.print(testee_out[0]+" ");
    			System.out.print(testee_out[1]+" ");
    			System.out.print(testee_out[2]+" ");
    			System.out.println(testee_out[3]);
        	   }
        }
     }
}


但是上面的Java代码最多只能拿22分(最后一个测试点通不过)!

原因是最有一个测试点的时长为前两个的 60~120倍,由于限时最多为200ms,所以只有前两个测试点的用时为 3.3ms以内,测试点四才可能通过

个人水平有限,不能写出使得前两个测试点在3.3ms内的Java代码,貌似不太可能实现吧~~~

只能说PAT 对 Java 是不友好的。




C++:


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
typedef struct student
{
    string num;
    int grade;
    int final_rank;
    int location_num;
    int local_rank;
    //注意以下这段的写法
    bool operator > (const student& s)const
    {
        if(grade != s.grade)
            return grade > s.grade;
        else return num < s.num;
    }
}student;
 
#define max 30000
#define INF 0x6FFFFFFF
vector<student> v;
 
int main()
{
    int n,m;
    int i,j;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>m;
        vector<student> temp(m);
        for(j=0; j<m; j++)
        {
            cin>>temp[j].num>>temp[j].grade;
            temp[j].location_num = i+1;
        }
        sort(temp.begin(),temp.end(),greater<student>());
        int nowGrade = INF;
        int nowRank = 0;
        for(j=0; j<m; j++)
        {
            
            //cout<<temp[j].num<<"   "<<temp[j].location_num<<endl;
            if(temp[j].grade == nowGrade)
                temp[j].local_rank = nowRank;
            else
            {
                temp[j].local_rank = j+1;
                nowRank = j + 1;
                nowGrade = temp[j].grade;
            }
            v.push_back(temp[j]);
        }
    }
    // 处理global_rank
    sort(v.begin(),v.end(),greater<student>());   //此处用到greater,头文件应该有#include<functional>
    int nowGrade = INF;
    int nowRank = 0;
    cout<<v.size()<<endl;
    for(i=0; i<v.size(); i++)
    {
        if(v[i].grade == nowGrade)
            v[i].final_rank = nowRank;
        else
        {
            nowRank = i+1; 
            v[i].final_rank = i+1;
            nowGrade = v[i].grade;
        }
        cout<<v[i].num<<" "<<v[i].final_rank<<" "<<v[i].location_num<<" "<<v[i].local_rank<<endl;
    }
 
    return 0;
}

C++代码出处:

http://www.2cto.com/kf/201308/239812.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值