PAT甲级1075 PAT Judge 测试点2&4 答案错误

这题是排序题,简单,但坑在细节。

题目说“For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist.”存在一种情况:用户pass the compiler 但是得分为0,还是得输出。所以不能根据总得分是否为0判断该不该被输出

有些细节的判断条件需要仔细理清。

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector> 
#include <cstdlib>

using namespace std;

struct user{
	string id;
	int test[5]={-2,-2,-2,-2,-2};
	int total=0;
	int numget;
	int perfect=0;
	int rank;
	int flag=0;
}peo[10010];

bool cmp(user a,user b){
	if(a.total!=b.total) return a.total>b.total;
	else if(a.perfect!=b.perfect) return a.perfect>b.perfect;
	else return a.id<b.id;
}

int main(){

	int numid,numq,numsub;
	cin>>numid>>numq>>numsub;

	int score[5]={0};
	for(int i=0;i<numq;i++){
		cin>>score[i];
	}

	for(int i=0;i<numsub;i++){
		string idstr;
		int qes,fenshu;
		cin>>idstr>>qes>>fenshu;
		int idint=stoi(idstr)-1;
		peo[idint].id=idstr;
		if(peo[idint].test[qes-1]<fenshu){
			if(fenshu!=-1){
				if(peo[idint].test[qes-1]>=0){
					peo[idint].total=peo[idint].total-peo[idint].test[qes-1]+fenshu;
				}
				else peo[idint].total=peo[idint].total+fenshu;
				peo[idint].flag=1;
			}
			

			peo[idint].test[qes-1]=fenshu;
			if(peo[idint].test[qes-1]==score[qes-1]){
				peo[idint].perfect++;
			}
		}		
	}
	
	sort(peo,peo+numid,cmp);

	peo[0].rank=1;
	for(int i=1;i<numid;i++){
		if(peo[i].total!=peo[i-1].total) peo[i].rank=i+1;
		else peo[i].rank=peo[i-1].rank;
	}

	for(int i=0;i<numid;i++){
		if(peo[i].total>0||peo[i].flag==1){
			cout<<peo[i].rank<<" "<<peo[i].id<<" "<<peo[i].total;
			for(int j=0;j<numq;j++){
				if(peo[i].test[j]>=0) cout<<" "<<peo[i].test[j];
				else if(peo[i].test[j]==-1) cout<<" 0";
				else  cout<<" -";
			}
			cout<<endl;
		}
			
	}
   
}

最后一点小贴士:
c++ string转int 用stoi()函数,头文件#include<string>.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值