PAT考试乙级1055(C++语言实现) (重点题目)(思路)

#include
#include
#include
using namespace std;
struct Student{
    char name[10];
    int height;
};

bool cmp(struct Student s1,struct Student s2){
     if(s1.height!=s2.height){       //身高不相同,身高矮的在前
        return s1.heightelse return strcmp(s1.name,s2.name)>0;//身高相同,比较名字,字典序大的在前
}

void output(Student s[],int begin,int end){
    Student temp[end-begin+2];//注意此处数组的大小
    int m=(end-begin)/2+1;  
    int count_left=0;       //左边到了第几个
    int count_right=0;      //右边到了第几个
    bool should_right=false;//是否该右边了
    for(int i=end;i>=begin;i--){
        if(i==end)  temp[m]=s[i];//先把最大值放在中间
        else{
            if(should_right){
                count_right++;
                temp[m+count_right]=s[i];
                should_right=false; 
            }
            else{
                count_left--;
                temp[m+count_left]=s[i];
                should_right=true;
            }       
        }   
    }
    for(int i=m+count_left;i<=m+count_right;i++){
        if(i!=(m+count_left))   cout<<" ";
        cout<cout<int main(){
    int N,K,i,j,last;
    Student s[10001];
    cin>>N>>K;
    for(i=1;i<=N;i++){
        cin>>s[i].name>>s[i].height;
    }
    sort(s+1,s+N+1,cmp);
    last=(K-1)*(N/K)+1;
    output(s,last,N);
    for(i=last-N/K;i>=1;i=i-N/K){
        output(s,i,i+N/K-1);
    }
    return 0;
}

代码参考:
http://blog.csdn.net/wanmeiwushang/article/details/51591972

你可能感兴趣的:(PAT)