Loading... #include<stdio.h> #include<stdlib.h> const MAX = 1500; //创建结构体 struct P{ int id; int time; }; //排序函数 int comp(const void *p1, const void *p2) { const struct P *ps1 = (struct P *)p1; const struct P *ps2 = (struct P *)p2; if(ps1->time != ps2->time) return ps1->time - ps2->time; else return ps1->id - ps2->id; } int main() { struct P p[MAX]; int count = 0; //声明总人数 int i, j; double sum = 0.0; scanf("%d", &count); for(i = 0; i < count; i++) //创建(id , time)散列表 { p[i].id = (i + 1); scanf("%d", &p[i].time); } qsort(p, count, sizeof(p[1]), comp); //快排 for(j = 0; j < count; j++) //贪心算法求和计算总等待时间 sum += p[j].time * (count - j - 1); for(j = 0; j < count; j++) //输出最优顺序 { printf("%d ",p[j].id); } printf("\n"); printf("%.2lf\n", sum / count); //输出平均等待时间 return 0; } [Python解法][1] [1]: http://mioe.xyz/index.php/archives/42/ Last modification:September 4, 2021 © Allow specification reprint Like 0 如果觉得我的文章对你有用,请随意赞赏
Comment here is closed