1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <math.h>
typedef struct Worker { char name[20]; int state; int workAge; double wage;
} Worker;
Worker *init(int num); int *sortWorker(int num, Worker *worker); bool isIn(int a, const int *p, int num);
int main(void) { int num = 0; Worker *worker = NULL; int *array = NULL; scanf("%d", &num);
worker = init(num); if (worker == NULL) { printf("ÉêÇë¿Õ¼äʧ°Ü\n"); exit(0); } for (int i = 0; i < num; i++) { scanf("%s %d %d %lf", worker[i].name, &worker[i].state, &worker[i].workAge, &worker[i].wage); }
array = sortWorker(num, worker);
for (int i = 0; i < num; i++) { printf("%s %d %d %.2f\n", worker[array[i]].name, worker[array[i]].state, worker[array[i]].workAge, worker[array[i]].wage); }
free(worker); free(array);
getchar();
return 0; }
Worker *init(int num) { Worker *worker = (Worker *)malloc(sizeof(Worker) * num);
return worker; }
int *sortWorker(int num, Worker *worker) { Worker w = worker[0]; int *label = (int *)malloc(sizeof(int) * num); int count = 0; label[count] = 0;
for (; count < num; count++) {
for (int i = 0; i < num; i++) { if (!isIn(i, label, count)) { if (w.state == worker[i].state) { if (fabs(w.wage - worker[i].wage) < 1e-10) { if (w.workAge < worker[i].workAge) { w = worker[i]; label[count] = i; } } else { if (w.wage > worker[i].wage) { w = worker[i]; label[count] = i; } } } else { if (w.state < worker[i].state) { w = worker[i]; label[count] = i; } } } }
for (int i = 0; i < num; i++) { if (!isIn(i, label, count)) { w = worker[i]; label[count + 1] = i; } } }
return label; }
bool isIn(int a, const int *p, int num) { for (int i = 0; i <= num; i++) { if (a == p[i]) { return true; } }
return false; }
|