文章

16

粉丝

33

获赞

2

访问

4.1k

头像
成绩排序 题解:
P1151 清华大学上机题
发布于2024年3月18日 00:17
阅读数 263

一开始想用map做,如下面所示,但是只能50%通过。

#include <cstdio>
#include <map>
#include <string>
#include <string.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <limits.h>
using namespace std;
map<string, int> chart;
int mode;

bool compare(string a,string b){
  if(mode==0){
    return chart[a]>chart[b];
  }else{
    return chart[a]<chart[b];
  }
}

string name[1000005];

int main(){
  int num;
  while(scanf("%d",&num)!=EOF){
    scanf("%d",&mode);

    int score;
    char temp[1000005];
    for(int i=0;i<num;i++){
      scanf("%s %d",temp,&score);
      name[i]=temp;
      chart[name[i]]=score;
    }
    sort(name,name+num,compare);
    for(int i=0;i<num;i++){
      printf("%s %d\n",name[i].c_str(),chart[name[i].c_str()]);
    }
  }

	return 0;
}

仔细思考了一下,可能是测试样例里面有同名不同分的人,那么map就没法用了,应...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发