重写·对比
纪录每次刷题的重写对比
改进:
#include <iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<math.h>
using namespace std;
typedef struct STU
{
string name;
int height;
}stu;
int next(int k,int juli,int num)
{
if(num==1)
{
return k/2+1;
}
else
{
int pos=k/2+1+juli*pow(-1,num+1);
if(pos>k)pos=k/2+1-juli;
return pos;
}
}
bool cmp(stu a,stu b)
{
if(a.height!=b.height)
{
return a.height>b.height;
}
else
{
return a.name<b.name;
}
}
stu s[10005];
string ans[1000];
int main()
{
int n,k;
cin>>n>>k;
int num=n/k;
int num_first=num+n%k;
int i;
for(i=1;i<=n;i++)
{
cin>>s[i].name;
cin>>s[i].height;
}
sort(s+1,s+n+1,cmp);
int len=0;
int pt=1;
for(i=1;i<=k;i++)
{
if(i==1)len=num_first;
else len=num;
int start=pt;
int endone=pt+len-1;
int mid=len/2+1;
for(int j=start;j<=endone;j++)
{
double q=j-start;
int pos=mid+pow(-1,q)*(1.0*q/2+0.5);
if(pos>len)pos=1;
ans[pos]=s[j].name;
}
for(int ss=1;ss<=len;ss++)
{cout<<ans[ss];
if(ss<len)cout<<" ";
}
if(i<k)
cout<<endl;
pt=endone+1;
}
return 0;
}
原来:
#include <iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<math.h>
using namespace std;
typedef struct STU
{
string name;
int height;
}stu;
int next(int k,int juli,int num)
{
if(num==1)
{
return k/2+1;
}
int pos=k/2+1+juli*pow(-1,num+1);
if(pos>k)pos=k/2+1-juli;
return pos;
}
}
bool cmp(stu a,stu b)
{
if(a.height!=b.height)
{
return a.height>b.height;
}
else
{
return a.name<b.name;
}
}
int main()
{
int zrs,ps;
cin>>zrs>>ps;
stu s[10005];
int i;
for(i=0;i<zrs;i++)
{
cin>>s[i].name;
cin>>s[i].height;
}
sort(s,s+zrs,cmp);
int k=zrs/ps;
int yushu=zrs%ps;
int k_last=k+yushu;
int first=0;
int last=0;
for(i=k;i>=1;i--)
{
int num=0;
if(i==k)num=k_last;
else num=k;
last=first+num-1;
int j=0;
string ans[15];
int mid=num/2+1;
int juli=0;
while(j<=num)
{
if(j==0)
{
j++;
ans[mid]=s[first].name;
first++;
juli++;
}
else
{
j++;
if(j>num)break;
int pos=next(num,juli,j);
ans[pos]=s[first].name;
first++;
j++;
if(j>num)break;
pos=next(num,juli,j);
ans[pos]=s[first].name;
first++;
juli++;
}
}
for(int q=1;q<num+1;q++)
{
cout<<ans[q];
if(q<num)cout<<" ";
}
cout<<endl;
}
return 0;
}