PAT 乙级 C++ 1055 集体照 (25分)

就是最后一排和非最后一排 都按照左右互博方式排列。

#include <cstdio>
#include <string>
#include <string.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

struct student{
	int shengao;
	char name[9];
}s;
char hashtable[11][10001][9]={0};
bool cmp(student a,student b){
	if(a.shengao!=b.shengao) return a.shengao>b.shengao;
	else return strcmp(a.name,b.name)<0;
}
int main(){
	int N,K,pm,lpm,zhongjian,cnt;			//总人数,行数 
	scanf("%d %d",&N,&K);
	vector <student> vec;
	for(int i=0; i<N; i++){
		scanf("%s %d",s.name,&s.shengao);
		vec.push_back(s);
	}
	pm=N/K;				//非最后一排的人数
	lpm=pm+N%K;			//最后一排的人数
	sort(vec.begin(),vec.end(),cmp); 
	vector <student> ::iterator it =vec.begin(); 
	for(int i=1; i<=K; i++){
		int x=-1;
		if(i==1){
			zhongjian= lpm/2+1;
			cnt= lpm;
		}else {
			zhongjian= pm/2+1;
			cnt= pm;
		}
		strcpy(hashtable[i][zhongjian],it->name);
		it++;
		for(cnt--;cnt>0;cnt--){
			zhongjian=zhongjian+x;			//中间+(-1)+(+2)+(-3);
			strcpy(hashtable[i][zhongjian],it->name);		//将string容器里的字符串copy到字符串。
			it++;
			x=(-1)*x/fabs(x)*(fabs(x)+1);	//乘以负一变换符号,x/|x|*(x+1)使x的绝对值加一。
		}
	}
	for(int i=1;i<=K;i++){
		if(i==1){
			for(int j=1;j<=lpm;j++){
				printf("%s",hashtable[i][j]);
				if(j!=lpm) printf(" ");
			}
		}else{
			for(int j=1;j<=pm;j++){
				printf("%s",hashtable[i][j]);
				if(j!=pm) printf(" ");
			}			
		}
		if(i!=K) printf("\n");
	}
	return 0;
}

转载须注明出处。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值