This is really an interesting question. I don't know what others think. In short, I think this question is really interesting and worth doing. Attach a question first:
There are lots of number games for children. these games are pretty easy to play but not so easy to make. we will discuss about an interesting game here. each player will be given N positive integer. (S) He can make a big integer by appending those integers after one another. such as if there are 4 integers as 123,124, 56, 90 then the following integers can be made-1231245690,124 1235690, 5612312490,901 2312456, 9056124123 etc. in fact 24 such integers can be made. but one thing is sure that 9056124123 is the largest possible integer which can be made.
You may think that it's very easy to find out the answer but will it be easy for a child who has just got the idea of number?
Input
Each input starts with a positive integer N (≤ 50). In next lines there are N positive integers. Input is terminated by N = 0, which shoshould not be processed.
Output
For each input set, you have to print the largest possible integer which can be made by appending all the N integers.
Sample Input
Output for Sample Input
4
123 124 56 90
5
123 124 56 90 9
5
9 9 9 9 9
0
9056124123
99056124123
99999
After reading this question, I will first think of finding the first largest number of N numbers in the first place, and if there are two numbers in the first place at the same time, the largest is the second place ,...... In this case, I will fall into a complicated discussion, wasting my brains. I have also gone through this process. But when I look back, I can think of a better solution.
After the problem is analyzed, the problem can be expressed as follows:
N positive numbers are given, and a certain method is used to sort the N numbers so that they are the largest number after being sorted.
The key to solving this problem is how to sort the N numbers? That is, the sorting method.
When it comes to sorting, we can use qsort, but the key to using qsort lies in the comparison function cmp called by qsort. This function cmp is used to compare the two positive numbers, which of them should be placed on the left, which is placed on the right.
The following describes how to implement cmp functions, which can be solved as follows: For two positive numbers I and j, we can write the numbers they can be combined, that is, ij and ji (note that ij represents a larger number when I and j are written in order), then compare ij AND ji, we can know that when we sort n positive numbers to satisfy the meaning of the question, if it contains I and j, then I must appear on the left of j or j must appear on the left of I.
Now the entire problem has been analyzed. For more information about how to implement it, see my problem-solving code, as shown below:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <string> # include <algorithm> using namespace std; char s [50] [100]; int cmp (const void * I, const void * j) {// determine which one to select when I and j are both optional, here we will write a comparison function for qsort to call char * a = (char *) I; char * B = (char *) j; char sak, sbk; int k, lena = strlen (a), lenb = strlen (B); for (k = 0; k <lena + lenb; k ++) {// cyclically determine whether the numbers ij and number ji are the same if (k <lena) sak = a [k]; else sak = B [k- Lena]; if (k <lenb) sbk = B [k]; else sbk = a [k-lenb]; if (sak! = Sbk) break;} if (k = lena + lenb) return 0; // ij and ji are the same, and return 0 indicates equal else {if (sak <sbk) return 1; // The Lexicographic Order of ij is small. If the return value is 1, select I and then select jelse return-1;} int main () {int N; while (cin> N & N! = 0) {for (int I = 0; I <N; I ++) cin> s [I]; qsort (s, N, sizeof (s [0]), cmp); for (int I = 0; I <N; I ++) cout <s [I]; cout <endl ;} return 0 ;}