weixin_39541632 于 2017.08.31 12:14 提问

2个回答

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   2017.08.31 17:43

`````` 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;
}
``````