其他分享
首页 > 其他分享> > PTA Basic Level 1055

PTA Basic Level 1055

作者:互联网

#include<bits/stdc++.h>
using namespace std;
typedef struct{
	char name[9];
	int height;
}stu;
int cmp(const void* a,const void* b){
	stu m=*(stu *)a;
	stu n=*(stu *)b;
	if (m.height==n.height)
		return strcmp(m.name,n.name);
	else 
		return n.height-m.height;
}

int main(){
	int n,k,r=1,index=0;
	cin>>n>>k;
	stu a[n]; 
	for (int i=0;i<n;i++){
		scanf("%s %d",a[i].name,&a[i].height);
	}
	qsort(a,n,sizeof(stu),cmp);
//	cout<<"---------------------------------------"<<endl;
//	cout<<"排序后"<<endl;
//	for (int i=0;i<n;i++){
//		printf("%s %d\n",a[i].name,a[i].height);
//	}
//	cout<<"---------------------------------------"<<endl;
	
	while (r<=k){
		int cnt_people;//代表该行的人数 
		if (r==1) cnt_people=n-n/k*(k-1);
		else cnt_people=n/k;
		int middle=cnt_people/2+1;//代表中间位置 
		stu ans[cnt_people];
		
		ans[middle-1]=a[index];
		int temp=index+1;
		for (int i=middle-2;i>=0;i--){//先填左边的 
			ans[i]=a[temp];
			temp+=2;
		}
		temp=index+2;
		for (int i=middle;i<cnt_people;i++){//再填右边的
			ans[i]=a[temp];
			temp+=2;
		}
		for (int i=0;i<cnt_people;i++){
			if (i!=0) printf(" ");
			printf("%s",ans[i].name);
		}
		index+=cnt_people;
		if (r!=k) cout<<endl;
		r++;
	}
	
	return 0;
}

标签:index,1055,temp,Level,int,PTA,height,stu,name
来源: https://blog.csdn.net/daydayupchen/article/details/119464836