Joining an array of numbers to form the biggest number
Arrange given numbers to form the biggest number
Arrange given numbers to form the biggest number
Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value. And if the given numbers are {1, 34, 3, 98, 9, 76, 45, 4}, then the arrangement 998764543431 gives the largest value.
int
myCompare(string X, string Y)
{
// first append Y at the end of X
string XY = X.append(Y);
// then append X at the end of Y
string YX = Y.append(X);
// Now see which of the two formed numbers is greater
return
XY.compare(YX) > 0 ? 1: 0;
}
// The main function that prints the arrangement with the largest value.
// The function accepts a vector of strings
void
printLargest(vector<string> arr)
{
// Sort the numbers using library sort funtion. The function uses
// our comparison function myCompare() to compare two strings.
// See http://www.cplusplus.com/reference/algorithm/sort/ for details
sort(arr.begin(), arr.end(), myCompare);
for
(
int
i=0; i < arr.size() ; i++ )
cout << arr[i];
}
Joining an array of numbers to form the biggest numberdef str_num_cmp(n1,n2):j1 = int(str(n1)+str(n2))j2 = int(str(n2)+str(n1))return j2-j1def biggest_num(num_array):snum_array = sorted(num_array, cmp = str_num_cmp)print "".join([str(num) for num in snum_array])