#include <iostream>
#include<cstdio>
#include<string>
#include<stdlib.h>
#include <algorithm>
using namespace std;
typedef struct
{
string name;
int height;
}person,*per;
bool compare(person a,person b)
{
return a.height > b.height;
}
person mysort(per p,int n)
{
sort(p,p+n,compare);
for(int i = 0;i<n-1;i++)
{
person temp;
if(p[i].height == p[i+1].height && p[i].name > p[i+1].name)
{
temp = p[i];
p[i] = p[i+1];
p[i+1] = temp;
}
}
}
int main()
{
person p[5] = {{"tom",188},{"mike",170},{"eva",168},{"alice",170},{"bob",170}};
int n = 5,k = 2;
mysort(p,n);
int m = n/k;
int fm = n/k + n%k;
string result = "";
for(int i = 0;i<n;)
{
if(i == 0)
{
result = p[i].name;
i++;
for(int j = 1;j<fm;j++,i++)
{
if(j%2 == 1)
result = p[i].name + " " + result;
else
result = result + " " + p[i].name;
}
cout<<result<<endl;
}
else
{
result = p[i].name;
i++;
for(int j = 1;j<m;j++,i++)
{
if(j%2 == 1)
result = p[i].name + " " + result;
else
result = result + " " + p[i].name;
}
cout<<result<<endl;
}
}
return 0;
}
最后卡在了格式输出部分上面,一度想用二叉树来存储name,最后中序遍历输出结果。然后看到网上的答案,直接拼接字符串,感觉智商受到了碾压。就酱!