2 weixin 39541632 weixin_39541632 于 2017.08.31 12:14 提问

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

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

2个回答

weixin_39541632
weixin_39541632   2017.08.31 12: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;
}

qq_30069409
qq_30069409   2017.08.31 17:43

你可以把数组中的每一个数字转换成string类型,之后进行快排。我这里谢了一个把数组排成最小的。跟着个同理吧。

 static bool cmp(string s1, string s2)
    {
        return (s1 + s2) < (s2 + s1);
    }
    string PrintMinNumber(vector<int> numbers)
    {
        vector <string> svec;
        char buf[256];
        for (vector<string>::size_type i = 0; i < numbers.size(); i++)
        {
            sprintf(buf, "%d", numbers[i]);
            string stemp = buf;
            svec.push_back(stemp);
        }
        sort(svec.begin(), svec.end(), cmp);
        string res = "";
        for (vector<string>::iterator iter = svec.begin();
        iter != svec.end(); iter++)
        {
            res += *iter;
        }
        return res;
    }
Csdn user default icon
上传中...
上传图片
插入图片