PAT1075. PAT Judge (25)

其中在排名输出上参照了

http://blog.csdn.net/xyzchenzd/article/details/27074665,原先自己写的很繁琐,而且还有一个测试点不通过。

#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
struct PATInfo
{
	int userId;
	int p[10];
	int sum;
	int flag;// is qualified to output
	int perfect;// full score num
	PATInfo(){
		for(int j=0;j<10;j++) p[j]=-2;
		sum=0;
		flag=0;
		perfect=0;
	}
};
int n,k,m;
int i,j;
int prob[10];
bool cmp(const PATInfo &a,const PATInfo &b){
	if(a.sum!=b.sum) return a.sum>b.sum;
	if(a.perfect!=b.perfect) return a.perfect>b.perfect;
	return a.userId<b.userId;
}
int main()
{

	cin>>n>>k>>m;
	PATInfo *userSet=new PATInfo[n+1];
	for(i=1;i<=k;i++) cin>>prob[i];
	for(i=1;i<=n;i++) userSet[i].userId=i;
	int uId,pId,obtainS;
	for(i=0;i<m;i++){
		cin>>uId>>pId>>obtainS;
		if(userSet[uId].p[pId]<obtainS){
			if(obtainS>=0){
				userSet[uId].sum+=obtainS;
				userSet[uId].flag=1;
			}
			if(userSet[uId].p[pId]>0){
				userSet[uId].sum-=userSet[uId].p[pId];
			}
			userSet[uId].p[pId]=obtainS;
			if(obtainS==prob[pId]){
				userSet[uId].perfect++;
			}
		}
	}
	sort(userSet+1,userSet+n+1,cmp);
	int score=userSet[0].sum;
	int t=1;
	for(i=1;i<=n;i++){
		if(userSet[i].flag==0) break;
		if(score!=userSet[i].sum){
			t=i;
			score=userSet[i].sum;
		}
		printf("%d %05d ",t,userSet[i].userId);
		cout<<userSet[i].sum;
		for(j=1;j<=k;j++){
			if(userSet[i].p[j]==-2){
				cout<<" -";
			}else if(userSet[i].p[j]==-1){
				cout<<" 0";
			}else{
				cout<<" "<<userSet[i].p[j];
			}
		}
		cout<<endl;
	}
	delete [] userSet;
	return 0;
}

	

  

 

posted @ 2016-04-09 19:58  Yellowman  阅读(507)  评论(0编辑  收藏  举报
TVRBMExqRXlPQzR5TXpjdU1UVTEK\n