文章

1

粉丝

87

获赞

0

访问

465

头像
成绩排序 有问题:数据通过率66%求助,用的是冒泡排序加了while判断还是不对
Se11 VIP
P1151 清华大学上机题
发布于2024年2月4日 13:03
阅读数 465

  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. typedef struct{
  5. char name[100];
  6. int num;
  7. }student;
  8.  
  9. int main(){
  10. int n, x;
  11. student stu[1000];
  12. while(scanf("%d %d",&n, &x) != EOF){
  13. for(int i = 0;i < n;i++){
  14. scanf("%s %d",stu[i].name,&stu[i].num);
  15. }
  16. student temp;
  17. for(int i = 0;i < n-1;i++)
  18. for(int j = 0;j < n-i-1;j++)
  19. if(x == 0)
  20. {
  21. if(stu[j+1].num > stu[j].num)
  22. {
  23. temp = stu[j];
  24. stu[j] = stu[j+1];
  25. stu[j+1] = temp;
  26. }
  27. }
  28. else{
  29. if(stu[j+1].num < stu[j].num){
  30. temp = stu[j];
  31. stu[j] = stu[j+1];
  32. stu[j+1] = temp;
  33. }
  34. }
  35. for(int i = 0;i < n;i++){
  36. printf("%s %d\n",stu[i].name,stu[i].num);
  37. }
  38. return 0;
  39. }
  40.  
  41. }
登录查看完整内容


登录后发布评论

1 条评论
snake VIP
2024年2月4日 15:35

应该是稳定性的问题,可以直接用stable_sort稳定排序

赞(1)