Group Photo(PAT)

 注意两点:

1.每行中间的最高,之后每行插入的人按照身高排序插入,由于是面对该Group,放在tallest person右边的人应插入到该person前面,即数组左边。

2.身高相同的情况,compare其名字,较小的优先插入。

利用sort排序,cmp比较需要仔细思考一下。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
struct node{
	string name;
	int height;
};
vector<node> v;
bool cmp(node a, node b){
	if(a.height!=b.height)
		return a.height<b.height;
	else
		return a.name>b.name;
}// define the compare function
int main(){
	int N,K;		
	cin>>N>>K;
	v.resize(N);
	for(int i=0;i<N;i++)
	{
		cin>>v[i].name;
		cin>>v[i].height;
	}
	sort(v.begin(),v.end(),cmp);
	int s=N/K;//to get the number of people of each row;
    int last=N-s*(K-1);// get the rear row's number
  	string rear[last];//to save the name order of the rear row
  	int count=0;
  	int m1=1;
  	int m2=1;
    for(int j=0;j<last;j++)
    	{	
    		if(j==0)
				rear[last/2]=v[N-1].name;	
			else{
				if(count%2==0){
					rear[last/2-m1]=v[N-1-j].name;
					m1++;
				}
				else
				{
					rear[last/2+m2]=v[N-1-j].name;
					m2++;
				}
				count++;
			}
		}
    for(int i=0;i<last;i++){
    	cout<<rear[i];
    	if(i!=last-1)
    		cout<<" ";
    	else
    	 	cout<<endl;
	}
    int now=N-1-last;
    for(int i=0;i<K-1;i++){
    	string temp[s];
    	int count1=0;
  		int m3=1;
  		int m4=1;
    	for(int j=0;j<s;j++){
    		if(j==0)
				temp[s/2]=v[now].name;
			else{
				if(count1%2==0){
					temp[s/2-m3]=v[now-j].name;
					m3++;
				}
				else
				{
					temp[s/2+m4]=v[now-j].name;
					m4++;
				}
				count1++;
			}
		}
    for(int m=0;m<s;m++){
    	cout<<temp[m];
    	if(m!=s-1)
    		cout<<" ";
    	else
    	 	cout<<endl;
	}	
    	now-=s;
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值