鱼C论坛

 找回密码
 立即注册
查看: 2002|回复: 2

[已解决]有没有更好的方法,对三个数排序,且可读性不能降低

[复制链接]
发表于 2019-9-20 00:08:40 | 显示全部楼层 |阅读模式
10鱼币
  1. #include <stdio.h>

  2. int main()
  3. {
  4.    
  5.     int a,b,c,t;
  6.     printf("请输入3个数,以逗号分隔:\n");
  7.     scanf("%d,%d,%d",&a,&b,&c);
  8.     if(a > b)
  9.     {
  10.         t = a;
  11.         a = b;
  12.         b = t;
  13.     }
  14.     if(a > c)
  15.     {
  16.         t = a;
  17.         a = c;
  18.         c = t;
  19.     }
  20.     if(b > c)
  21.     {
  22.         t = b;
  23.         b = c;
  24.         c = t;
  25.     }
  26.     printf("从小到大排列依次是:%d,%d,%d",a,b,c);

  27.     return 0;
  28. }
复制代码
最佳答案
2019-9-20 00:08:41
我上面用了qsort这个现成的函数不知道行不行
不过不管是qsort还是现在这个sort,目的就是排序,只要能完成任务,不管用什么算法都可以

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int compar(const void *a, const void *b)
  5. {
  6.         return *(const int *)a > *(const int *)b;
  7. }

  8. void sort(void *arr, size_t arr_size, size_t elem_size, int (*compar)(const void *a, const void *b))
  9. {
  10.         void *buf = malloc(elem_size);
  11.         for(size_t i = 0; i < arr_size; ++i)
  12.         {
  13.                 for(size_t j = i + 1; j < arr_size; ++j)
  14.                 {
  15.                         void *a = arr + i * elem_size;
  16.                         void *b = arr + j * elem_size;
  17.                         if(compar(a, b))
  18.                         {
  19.                                 memcpy(buf, a, elem_size);
  20.                                 memcpy(a, b, elem_size);
  21.                                 memcpy(b, buf, elem_size);
  22.                         }
  23.                 }
  24.         }
  25.         free(buf);
  26. }

  27. void sort_3(int *a, int *b, int *c)
  28. {
  29.         int arr[3] = {*a, *b, *c};
  30.         //qsort(arr, 3, sizeof(int), compar);
  31.         sort(arr, 3, sizeof(int), compar);
  32.         *a = arr[0];
  33.         *b = arr[1];
  34.         *c = arr[2];
  35. }

  36. int main()
  37. {
  38.         int a = 8, b = 2, c = 5;
  39.         sort_3(&a, &b, &c);
  40.         printf("%d %d %d\n", a, b, c);
  41.         return 0;
  42. }
复制代码

最佳答案

查看完整内容

我上面用了qsort这个现成的函数不知道行不行 不过不管是qsort还是现在这个sort,目的就是排序,只要能完成任务,不管用什么算法都可以
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-20 00:08:41 | 显示全部楼层    本楼为最佳答案   
我上面用了qsort这个现成的函数不知道行不行
不过不管是qsort还是现在这个sort,目的就是排序,只要能完成任务,不管用什么算法都可以

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int compar(const void *a, const void *b)
  5. {
  6.         return *(const int *)a > *(const int *)b;
  7. }

  8. void sort(void *arr, size_t arr_size, size_t elem_size, int (*compar)(const void *a, const void *b))
  9. {
  10.         void *buf = malloc(elem_size);
  11.         for(size_t i = 0; i < arr_size; ++i)
  12.         {
  13.                 for(size_t j = i + 1; j < arr_size; ++j)
  14.                 {
  15.                         void *a = arr + i * elem_size;
  16.                         void *b = arr + j * elem_size;
  17.                         if(compar(a, b))
  18.                         {
  19.                                 memcpy(buf, a, elem_size);
  20.                                 memcpy(a, b, elem_size);
  21.                                 memcpy(b, buf, elem_size);
  22.                         }
  23.                 }
  24.         }
  25.         free(buf);
  26. }

  27. void sort_3(int *a, int *b, int *c)
  28. {
  29.         int arr[3] = {*a, *b, *c};
  30.         //qsort(arr, 3, sizeof(int), compar);
  31.         sort(arr, 3, sizeof(int), compar);
  32.         *a = arr[0];
  33.         *b = arr[1];
  34.         *c = arr[2];
  35. }

  36. int main()
  37. {
  38.         int a = 8, b = 2, c = 5;
  39.         sort_3(&a, &b, &c);
  40.         printf("%d %d %d\n", a, b, c);
  41.         return 0;
  42. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-20 00:27:14 | 显示全部楼层
我不知道这样有没有降低可读性

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int compar(const void *a, const void *b)
  4. {
  5.         return *(const int *)a > *(const int *)b;
  6. }

  7. void sort_3(int *a, int *b, int *c)
  8. {
  9.         int arr[3] = {*a, *b, *c};
  10.         qsort(arr, 3, sizeof(int), compar);
  11.         *a = arr[0];
  12.         *b = arr[1];
  13.         *c = arr[2];
  14. }

  15. int main()
  16. {
  17.         int a = 8, b = 2, c = 5;
  18.         sort_3(&a, &b, &c);
  19.         printf("%d %d %d\n", a, b, c);
  20.         return 0;
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 05:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表