1015 德才论

来源:互联网 发布:美国情景喜剧排行知乎 编辑:程序博客网 时间:2024/06/08 18:32
#include <stdio.h>#include <malloc.h>typedef struct{  int sno;  int moral;  int talent;  int sumscore;}STUDENT;char Category(const STUDENT * stu, int pass, int good);  void PrintStu(const STUDENT stu[], int num);      void QuickSortStu(STUDENT stu[], int low, int high);  int main(void){  int num, pass, good;          scanf("%d %d %d", &num, &pass, &good);  getchar();                int A = 0;      int B = 0;    int C = 0;    int D = 0;    STUDENT * stuA = (STUDENT *)malloc((num + 1) * sizeof(STUDENT));    STUDENT * stuB = (STUDENT *)malloc((num + 1) * sizeof(STUDENT));  STUDENT * stuC = (STUDENT *)malloc((num + 1) * sizeof(STUDENT));  STUDENT * stuD = (STUDENT *)malloc((num + 1) * sizeof(STUDENT));  STUDENT temp;  for (int i = 0; i < num; i++)  {    scanf("%d %d %d", &temp.sno, &temp.moral, &temp.talent);    temp.sumscore = temp.moral + temp.talent;    switch (Category(&temp, pass, good))    {    case 'A':      stuA[++A] = temp;      break;    case 'B':      stuB[++B] = temp;      break;    case 'C':      stuC[++C] = temp;      break;    case 'D':      stuD[++D] = temp;      break;    default:      break;    }  }  QuickSortStu(stuA, 1, A);    QuickSortStu(stuB, 1, B);  QuickSortStu(stuC, 1, C);  QuickSortStu(stuD, 1, D);  printf("%d\n", A + B + C + D);    PrintStu(stuA, A);        PrintStu(stuB, B);  PrintStu(stuC, C);  PrintStu(stuD, D);  free(stuA);  free(stuB);  free(stuC);  free(stuD);  return 0;}char Category(const STUDENT * stu, int pass, int good){  if ((*stu).moral >= good && (*stu).talent >= good)    return 'A';  else if ((*stu).moral >= good && (*stu).talent >= pass)    return 'B';  else if ((*stu).moral >= pass && (*stu).talent >= pass && (*stu).moral >= (*stu).talent)    return 'C';  else if ((*stu).moral < pass || (*stu).talent < pass)    return 'Z';  else    return 'D';}void PrintStu(const STUDENT stu[], int num){  for (int i = 1; i <= num; i++)    printf("%d %d %d\n", stu[i].sno, stu[i].moral, stu[i].talent);}void QuickSortStu(STUDENT stu[], int low, int high){  if (low >= high)    return;  int first = low;  int last = high;  stu[0] = stu[first];  while (first < last)  {    while (first < last &&      !(      (stu[last].sumscore > stu[0].sumscore) ||      (stu[last].sumscore == stu[0].sumscore && stu[last].moral > stu[0].moral) ||      (stu[last].sumscore == stu[0].sumscore && stu[last].moral == stu[0].moral && stu[last].sno < stu[0].sno)      )      )      last--;    stu[first] = stu[last];    while (first < last &&      !(      (stu[first].sumscore < stu[0].sumscore) ||      (stu[first].sumscore == stu[0].sumscore && stu[first].moral < stu[0].moral) ||      (stu[first].sumscore == stu[0].sumscore && stu[first].moral == stu[0].moral && stu[first].sno > stu[0].sno)      )      )      first++;    stu[last] = stu[first];  }  stu[first] = stu[0];  QuickSortStu(stu, low, first - 1);  QuickSortStu(stu, first + 1, high);}
0 0
原创粉丝点击