我试图从一组整数中组合出可能的最大数.该代码只有在输入为个位数时才有效,否则它会给我错误的答案.例如,如果输入是2,21,它会给我212而不是221.我试图编辑代码,但我卡住了.我有一个 idea ,将用户输入的任何数字转换为数字,即210->;2,1,0.但我不知道如何实现它.
int IsGreaterOrEqual(int n1, int n2)
{
}
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element
// with the first element
swap(&arr[min_idx], &arr[i]);
}
}
int main()
{
int n; //number of elements
scanf("%d", &n);
int ints[n], sol[n];
int size = sizeof(ints)/sizeof(ints[0]);
for(int i = 0; i < n; i++){ //taking the inputs from the user
scanf("%d", &ints[i]);
}
ints[n] = selectionSort(ints, size); //sort the array in ascending order
for(int i = n-1; i >= 0; i--){ //here is where I am stuck
}
}
我将使用IsGreaterOrEqual函数向前和向后追加2个数字,并返回最大的数字.例如,附加2和21->;221和212.