#116高考排名 可复制粘贴代码

chen_zhe 沙雕 2020-06-21 17:35:36 2020-06-21 18:47:15 0

嗯,鉴于liuser的代码是图片不能复制并且抄起来代码多,我重新写了一个简洁版的代码,欢迎需要的童鞋们复制粘贴!

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct stu{
    char num[15];
    char name[15];
    int rank;
    int score;
}a[1000010];
bool operator < (const stu a,const stu b){
    return a.score>b.score;
}
bool cmp(const stu a,const stu b){
    return strcmp(a.num,b.num)<0;
}
stu f;
int main(){
    int n,m,p;
    freopen("A.in","r",stdin);
    freopen("A.out","w",stdout);
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    scanf("%s%s%d",a[i].num,a[i].name,&a[i].score);
    sort(a+1,a+n+1);
    a[1].rank=1;
    for(int i=2; i<=n; i++)
    if(a[i-1]<a[i]) a[i].rank=a[i-1].rank+1;
    else a[i].rank=a[i-1].rank;
    sort(a+1,a+n+1,cmp);
    scanf("%d",&m);
    while(m--){
        scanf("%s",f.num);
        p=lower_bound(a+1,a+n+1,f,cmp)-a;
        if(strcmp(a[p].num,f.num)==0) printf("%s %d\n",a[p].name,a[p].rank);
        else printf("Test error number!\n");
    }
}
{{ vote && vote.total.up }}

共 5 条回复

CPP 刷题王

wyy

ydf23

tlqtj, jbl

Rain5

Rain5

讨论 全局板块 #116高考排名 可复制粘贴代码 chen_zhe沙雕 2020-06-21 17:35:36 2020-06-21 18:47:15

嗯,鉴于liuser的代码是图片不能复制并且抄起来代码多,我重新写了一个简洁版的代码,欢迎需要的童鞋们复制粘贴!

#include #include #include using namespace std; struct stu{ char num[15]; char name[15]; int rank; int score; }a[1000010]; bool operator < (const stu a,const stu b){ return a.score>b.score; } bool cmp(const stu a,const stu b){ return strcmp(a.num,b.num)<0; } stu f; int main(){ int n,m,p; freopen("A.in","r",stdin); freopen("A.out","w",stdout); scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%s%s%d",a[i].num,a[i].name,&a[i].score); sort(a+1,a+n+1); a[1].rank=1; for(int i=2; i<=n; i++) if(a[i-1]<a[i]) a[i].rank=a[i-1].rank+1; else a[i].rank=a[i-1].rank; sort(a+1,a+n+1,cmp); scanf("%d",&m); while(m--){ scanf("%s",f.num); p=lower_bound(a+1,a+n+1,f,cmp)-a; if(strcmp(a[p].num,f.num)==0) printf("%s %d\n",a[p].name,a[p].rank); else printf("Test error number!\n"); } } 共 2 条回复

Duke 2020-06-21 18:41:33 题解是要解释这个是为什么这么干和有什么用的。。。

Duke 2020-06-21 18:40:45 #include<bits/stdc++.h> using namespace std; struct stu{ char mame[15]; char kh[10]; int id; int score; }a[100010]; bool cmp1(stu x,stu y){ return x.score>y.score; } bool cmp2(stu x,stu y){ if(strcmp(x.kh,y.kh)<0)return true; else return false; } int main() { freopen("A.in","r",stdin); freopen("A.out","w",stdout); int n,m; cin>>n; for(int i=1;i<=n;i++){ scanf("%s%s%d",a[i].kh,a[i].mame,&a[i].score); } sort(a+1,a+1+n,cmp1); int x=1; a[1].id=x; for(int i=2;i<=n;i++){ if(a[i].score==a[i-1].score){ a[i].id=a[i-1].id; } else { a[i].id=a[i-1].id+1; }

}
sort(a+1,a+1+n,cmp2);
cin>>m;
stu h;
while(m--){
	scanf("%s",h.kh);
	int p=lower_bound(a+1,a+1+n,h,cmp2)-a;
	if(strcmp(a[p].kh,h.kh)==0){
		printf("%s %d\n",a[p].mame,a[p].id);
	}
	else {
		printf("Test error number!\n");
	}
}
return 0;

} Liuser's OJ powered by SYZOJ DIY by woshiluo AHSFNU 帮助

Duke

题解是要解释这个是为什么这么干和有什么用的。。。

Duke
#include<bits/stdc++.h>
using namespace std;
struct stu{
	char mame[15];
	char kh[10];
	int id;
	int score;
}a[100010];
bool cmp1(stu x,stu y){
	return x.score>y.score;
}
bool cmp2(stu x,stu y){
	if(strcmp(x.kh,y.kh)<0)return true;
	else return false;
}
int main()
{
	freopen("A.in","r",stdin);
	freopen("A.out","w",stdout);
	int n,m;
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%s%s%d",a[i].kh,a[i].mame,&a[i].score);
	}
	sort(a+1,a+1+n,cmp1);
	int x=1;
	a[1].id=x;
	for(int i=2;i<=n;i++){
		if(a[i].score==a[i-1].score){
			a[i].id=a[i-1].id;
		}
		else {
			a[i].id=a[i-1].id+1;
		}
		
	}
	sort(a+1,a+1+n,cmp2);
	cin>>m;
	stu h;
	while(m--){
		scanf("%s",h.kh);
		int p=lower_bound(a+1,a+1+n,h,cmp2)-a;
		if(strcmp(a[p].kh,h.kh)==0){
			printf("%s %d\n",a[p].mame,a[p].id);
		}
		else {
			printf("Test error number!\n");
		}
	}
	return 0;
}