Topic:
There are n positive integers, which are concatenated into a row to form one of the largest multi-bit integers.
For example: n=3, 3 integers 13,312,343, the maximum number of integers is 34331213.
Also such as: n=4, 4 integer 7,13,4,246, the largest integer is 7424613.
Input:
N
Enter the number of N.
Output:
The number of multiple digits connected.
Ideas:
Let the number of rows be concatenated to the largest integer, so consider the individual integer order, so what are the criteria for sorting these numbers?
Guideline: Set two integers is a, a, if a+b is greater than b+a, then a is in front of B, whereas B in front of a is the local solution
The global solution is the order of the connections for all integers, since a connection is used to use a string.
#include <iostream>#include<string>#include<vector>#include<sstream>using namespacestd;stringstr[3] = {""};stringGetstrfromint (intN) {StringStream ss;stringS;ss<<N;ss>>s;returns;}stringGreedyalgorithm (intN) {intarr[4] = {0 };stringStrmax =""; cout<<"Please Input the Data:"<<Endl;intm =N; while(m--){if(! (Cin >>Arr[m])) {cout<<"input data not valid"<<Endl;returnNULL;}}//The core algorithm ——— convert integers to strings and compare them together for(inti =0; I < n; ++i) {Str[i]=Getstrfromint (Arr[i]);} for(inti =0; I < n; ++i) { for(intj = i +1; J < N; ++j) {if((Str[i]+str[j]). Compare (Str[j] + str[i]) <0){stringtemp=Str[i];str[i]=Str[j];str[j]=temp; }}} for(inti =0; I < n; ++i) {Strmax+=str[i];}returnStrmax;}intMain () {intN;cout<<"Please Input Datas:"<<Endl;if(Cin >>N) cout<< greedyalgorithm (n) <<Endl;Elsecout<<"input data not valid"<<Endl;return 0;}
C + + Knowledge points Summary:
① converts an integer to a string: Requires header file # include <sstream> specific code as follows:
string Getstrfromint (int n)
{
StringStream SS;
string S;
SS << N;
SS >> S;
return s;
}
② string Connection: A variable of type string can be connected directly with "+", "+ =".
③ string Comparisons :
String strings can be directly compared with "<", ">".
int strcmp (const char firststring[], const char secondstring); ( character by comparison )
"Greedy algorithm" maximum integer