版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sam_ONE/article/details/83791040
将一个数组中的各个元素拼接组合,得到数字最大的组合。
如:int[] arr = new int[5] {1, 26, 56, 894, 5648};
拼接组合后最大的数为 564889456261 --> 5648 894 56 26 1 的拼接结构
public string GetArraySortMaxNumber(int[] nums)
{
int temp;
for (int i = 1; i < nums.Length; i++)
{
temp = nums[i];
for (int j = i - 1; j >= 0; j--)
{
bool insert = true;
string a = nums[j].ToString();
string b = temp.ToString();
//36 365 -- 36 367 的两种特殊情况(处理如下)
//补齐短的数字至和长的数字长度相同,补上的数字位短的数字的最后一位数
if (a.Length > b.Length)
{
b = AddLastStr(a, b);
}
else if (a.Length < b.Length)
{
a = AddLastStr(b, a);
}
for (int x = 0; x < a.Length; x++)
{
int aNum = a[x];
int bNum = b[x];
if (bNum > aNum)
insert = false;
}
//使用插入排序
if (!insert)
{
nums[j + 1] = nums[j];
if (j == 0)
{
nums[0] = temp;
break;
}
}
else
{
nums[j + 1] = temp;
break;
}
}
}
string result = string.Empty;
foreach (var item in nums)
{
result += item;
}
return result;
}