标签:
问题描述:
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印出拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这3个数字能拍成的最小数字321323。
C++代码:
void PrintMinNumber(int* numbers, int length)
{
if(numbers == NULL || length <= 0)
return;
stringstream ss;//用来数字和字符串转换;
string* str=new string[length];
for(int i = 0; i < length; ++i)
{
ss.clear();
ss<<numbers[i];
ss>>str[i];
}
sort(str,str+length, compare);
for(int i = 0; i < length; ++i)
cout << str[i];
printf("\n");
}
bool compare(const string strNumber1, const string strNumber2)
{
string str1 = strNumber1+strNumber2;
string str2 = strNumber2+strNumber1;
return str2>str1;
}
标签:
原文地址:http://blog.csdn.net/leosha/article/details/46475171