鱼C论坛

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

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

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

int main()
{
    
    int a,b,c,t;
    printf("请输入3个数,以逗号分隔:\n");
    scanf("%d,%d,%d",&a,&b,&c);
    if(a > b)
    {
        t = a;
        a = b;
        b = t;
    }
    if(a > c)
    {
        t = a;
        a = c;
        c = t;
    }
    if(b > c)
    {
        t = b;
        b = c;
        c = t;
    }
    printf("从小到大排列依次是:%d,%d,%d",a,b,c);

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

int compar(const void *a, const void *b)
{
        return *(const int *)a > *(const int *)b;
}

void sort(void *arr, size_t arr_size, size_t elem_size, int (*compar)(const void *a, const void *b))
{
        void *buf = malloc(elem_size);
        for(size_t i = 0; i < arr_size; ++i)
        {
                for(size_t j = i + 1; j < arr_size; ++j)
                {
                        void *a = arr + i * elem_size;
                        void *b = arr + j * elem_size;
                        if(compar(a, b))
                        {
                                memcpy(buf, a, elem_size);
                                memcpy(a, b, elem_size);
                                memcpy(b, buf, elem_size);
                        }
                }
        }
        free(buf);
}

void sort_3(int *a, int *b, int *c)
{
        int arr[3] = {*a, *b, *c};
        //qsort(arr, 3, sizeof(int), compar);
        sort(arr, 3, sizeof(int), compar);
        *a = arr[0];
        *b = arr[1];
        *c = arr[2];
}

int main()
{
        int a = 8, b = 2, c = 5;
        sort_3(&a, &b, &c);
        printf("%d %d %d\n", a, b, c);
        return 0;
}

最佳答案

查看完整内容

我上面用了qsort这个现成的函数不知道行不行 不过不管是qsort还是现在这个sort,目的就是排序,只要能完成任务,不管用什么算法都可以
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

int compar(const void *a, const void *b)
{
        return *(const int *)a > *(const int *)b;
}

void sort(void *arr, size_t arr_size, size_t elem_size, int (*compar)(const void *a, const void *b))
{
        void *buf = malloc(elem_size);
        for(size_t i = 0; i < arr_size; ++i)
        {
                for(size_t j = i + 1; j < arr_size; ++j)
                {
                        void *a = arr + i * elem_size;
                        void *b = arr + j * elem_size;
                        if(compar(a, b))
                        {
                                memcpy(buf, a, elem_size);
                                memcpy(a, b, elem_size);
                                memcpy(b, buf, elem_size);
                        }
                }
        }
        free(buf);
}

void sort_3(int *a, int *b, int *c)
{
        int arr[3] = {*a, *b, *c};
        //qsort(arr, 3, sizeof(int), compar);
        sort(arr, 3, sizeof(int), compar);
        *a = arr[0];
        *b = arr[1];
        *c = arr[2];
}

int main()
{
        int a = 8, b = 2, c = 5;
        sort_3(&a, &b, &c);
        printf("%d %d %d\n", a, b, c);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-20 00:27:14 | 显示全部楼层
我不知道这样有没有降低可读性
#include <stdio.h>
#include <stdlib.h>

int compar(const void *a, const void *b)
{
        return *(const int *)a > *(const int *)b;
}

void sort_3(int *a, int *b, int *c)
{
        int arr[3] = {*a, *b, *c};
        qsort(arr, 3, sizeof(int), compar);
        *a = arr[0];
        *b = arr[1];
        *c = arr[2];
}

int main()
{
        int a = 8, b = 2, c = 5;
        sort_3(&a, &b, &c);
        printf("%d %d %d\n", a, b, c);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-4 09:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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