Jia ming 2017-08-31 04:14 采纳率: 0%
浏览 727

能否用C语言解决这个问题?

编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021?

  • 写回答

2条回答 默认 最新

  • Jia ming 操作系统领域优质创作者 2017-08-31 04:15
    关注

    /*组成最大数字*/
    #include
    #include
    #include
    int num[20], number;

    struct MyStruct
    {
    int x, y;
    }MyStruct[20], temp_struct;

    void Random()
    {
    for (int i = 0; i < 15; i++)
    {
    num[i] = rand() % 1000;
    //scanf("%d", &num[i]);
    MyStruct[i].x = num[i];
    number += 1;
    }
    //printf("%d\n", rand() % 1000);
    }

    /*传入数字x,需要判断位数y,z==1 得到这个数字的总位数。返回分割后的数字*/
    int Divide(int x, int y, int z)
    {
    int substitute_x = x, i = 1;
    while (substitute_x>=10) {substitute_x /= 10;i += 1;}
    int wei = i;
    if (z == 1)
    return wei;
    //printf("%d位数为:%d\n",x,i);

    for (int i = 1; i <= wei - y; i++)  {x /= 10;}
    //printf("结果是:%d", x);
    return x;
    

    }

    /*分区处理函数,数组的起始位置x,结束位置y*/
    void Piece(int x, int y)
    {
    for (int j = x; j < y ; j++)
    {
    for (int i = x; i < x + y - j; i++)
    {
    if (Divide(MyStruct[i].x, 1, 1) > Divide(MyStruct[i + 1].x, 1, 1))//233 23
    {
    if (Divide(MyStruct[i].x, Divide(MyStruct[i + 1].x, 1, 1), 0) < MyStruct[i + 1].x)
    temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;
    else if (Divide(MyStruct[i].x, Divide(MyStruct[i + 1].x, 1, 1), 0) == MyStruct[i + 1].x)
    {
    /*if (MyStruct[i].x % 10 < Divide(MyStruct[i + 1].x, 1, 0))
    temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;*/
    if (MyStruct[i + 1].x * 10 + Divide(MyStruct[i].x, 1, 0) > Divide(MyStruct[i].x, (Divide(MyStruct[i + 1].x, 1, 1)) + 1, 0))
    temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;
    }
    }

            else if (Divide(MyStruct[i].x, 1, 1) < Divide(MyStruct[i + 1].x, 1, 1))//6 629
            {
                if (MyStruct[i].x < Divide(MyStruct[i + 1].x, Divide(MyStruct[i].x, 1, 1), 0))
                    temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;
                else if (MyStruct[i].x == Divide(MyStruct[i + 1].x, Divide(MyStruct[i].x, 1, 1), 0))//45 456
                {
                    /*if (MyStruct[i+1].x % 10 > Divide(MyStruct[i].x, 1, 0))
                        temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;*/
                    if (MyStruct[i].x * 10 + Divide(MyStruct[i + 1].x, 1, 0) < Divide(MyStruct[i + 1].x, (Divide(MyStruct[i].x, 1, 1)) + 1, 0))
                        temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;
                }
            }
    
            else
            {
                if (MyStruct[i].x<MyStruct[i+1].x)
                    temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;
            }
        }
    }
    

    }

    /*处理函数*/
    void Operate()
    {
    for (int i = 0; i < number; i++) { MyStruct[i].y = Divide(num[i], 1, 0); }
    //for (int i = 0; i < number; i++) printf("%d ", substitute_num[i]);

    for (int j = 0; j < number - 1; j++)
    {
        for (int i = 0; i < number - 1 - j; i++)
        {
            if (MyStruct[i].y < MyStruct[i + 1].y)
                temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;
        }
    }
    
    /*for (int i = 0; i < number; i++)
    {
        printf("%d ", MyStruct[i].y);
    }
    return;*/
    
    for (int i = 0; i < number;)
    {
        int get = MyStruct[i].y, xx = i;
    
        while (MyStruct[i].y == get)    
            i++;
    
        int yy = i - 1;
        Piece(xx, yy);
        /*if (Divide(MyStruct[i].x, 1, 1) == Divide(MyStruct[i + 1].x, 1, 1) && MyStruct[i].y == MyStruct[i + 1].y)
            temp_struct = MyStruct[i], MyStruct[i] = MyStruct[i + 1], MyStruct[i + 1] = temp_struct;*/
    }
    
    for (int i = 0; i < number; i++)
        printf("%d ", MyStruct[i].x);
    

    }

    int main()
    {
    for (int i = 0; i < 100 ; i++)
    {
    Random();
    Operate();
    printf("\n--------------------------------------------------------------\n");
    number = 0;
    }
    //Divide(num[0], 2);
    return 0;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 JS定时器制作实时时间计数器
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据
  • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)