是一种比较呆的想法,算是最笨的一种了
思想就是在读入数据的时候就分类存储,然后对每一类进行分类
#include <iostream>
struct student
{
long studentID;
int moral;
int talent;
};
struct student class_1[100000];
struct student class_2[100000];
struct student class_3[100000];
struct student class_4[100000];
void inputStudent(struct student *p,long x,int y,int z);
void printStudent(struct student p[],long index);
void sortStudent(struct student p[],long left,long right);
long sortStudentClass(struct student p[],long left,long right);
void swapStudent(struct student *p1,struct student *p2);
bool islower(struct student *p1,struct student *p2);
int main(void)
{
long N = 0;
int L = 0,H = 0;
long num = 0;
int mor = 0,tal = 0;
scanf("%ld %d %d",&N,&L,&H);
long numberClass1 = 0,numberClass2 = 0, numberClass3 = 0,numberClass4 = 0;
long index = 0;
while (index < N)
{
if(scanf("%ld %d %d",&num,&mor,&tal) == 3){
if ((mor >= L) && (tal >= L))
{
if ((mor >= H) && (tal >= H))
inputStudent(&class_1[numberClass1++],num,mor,tal);
else if ((mor >= H) && (tal < H))
inputStudent(&class_2[numberClass2++],num,mor,tal);
else if ((mor < H) && (mor >= tal))
inputStudent(&class_3[numberClass3++],num,mor,tal);
else
inputStudent(&class_4[numberClass4++],num,mor,tal);
}
index++;
}
}
printf("%ld\n",numberClass1 + numberClass2 + numberClass3 + numberClass4);
if(numberClass1){sortStudent(class_1,0,numberClass1 - 1);
printStudent(class_1,numberClass1);}
if(numberClass2) {sortStudent(class_2,0,numberClass2 - 1);
printStudent(class_2,numberClass2);}
if(numberClass3) {sortStudent(class_3,0,numberClass3 - 1);
printStudent(class_3,numberClass3);}
if(numberClass4) {sortStudent(class_4,0,numberClass4 - 1);
printStudent(class_4,numberClass4);}
return 0;
}
void inputStudent(struct student *p,long x,int y,int z)
{
p->studentID = x;
p->moral = y;
p->talent = z;
}
void printStudent(struct student p[],long index)
{
long n = 0;
while (n < index)
{
printf("%ld %d %d\n",p[n].studentID,p[n].moral,p[n].talent);
n++;
}
}
void sortStudent(struct student p[],long left,long right)
{
if (left >= right) return;
long solider = sortStudentClass(p,left,right);
sortStudent(p,left, solider - 1);
sortStudent(p,solider + 1,right);
}
long sortStudentClass(struct student p[],long left,long right)
{
long solider = left;
long i = left;
long j = right;
while (i < j)
{
while ((i < j) && islower(&p[j],&p[solider])) j--;
while ((i < j) && !islower(&p[i],&p[solider])) i++;
if (i != j) swapStudent(&p[i],&p[j]);
}
if (i == j) swapStudent(&p[i],&p[solider]);
return i;
}
void swapStudent(struct student *p1,struct student *p2)
{
struct student p;
p = *p1;
*p1 = *p2;
*p2 = p;
}
bool islower(struct student *p1,struct student *p2)
{
if ((p1->moral + p1->talent) == (p2->moral + p2->talent))
{
if (p1->moral < p2->moral) return true;
else if ((p1->moral == p2->moral) && (p1->studentID > p2->studentID)) return true;
else return false;
}
else if ((p1->moral + p1->talent) < (p2->moral + p2->talent))
return true;
else
return false;
}
总的来说感觉还是比较好想的,排序的就是把条件变化了一下,总的来说还是快速排序的思想