Greedy algorithm--n number of connections to get the smallest or largest number of multi-bit integers
has n positive integers that connect them in 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 concatenated into the largest integer is 7424613
Input: N
N Number
Output: Number of connections to multiple digits
Algorithm analysis: This problem is very easy to think of using greedy method, in the exam there are many students to the whole number from the big to the small sequence of connections, test questions are also in line with the example, but the final test results are not all right. According to this greedy standard, we can easily find a counter example: 12,121 should be composed of 12121 instead of 12112, then is not mutually inclusive when the small to large. Not necessarily, such as: 12,123 is 12312 instead of 12112, so there are many kinds of things. Is not this question can not use greedy law.
In fact, the problem is can be greedy method to solve, just the greedy standard is wrong, the right greedy standard is: the first integer into a string, and then compare a+b and B+a, if A+b>b+a, put a in front of B, vice versa is a row in the back of B.
Examples of normal string comparison defects are described. such as: a= ' 321 ', b= ' 32 ', according to the standard string comparison rules because a>b, so a+b > B+a, and actually ' 32132 ' < ' 32321 '.
So, customize the comparison rule for a string: that is, if a+b>b+a, we think a>b. And can prove: if a+b>=b+a,b+c>=c+b, then there must be: A+c>=c+a.
In this way, the program is very simple. In 3 steps, the n numbers are converted to string storage, the n strings are sorted by custom rules, and the strings are output in order from small to large (the largest multi-bit integer if large to small).
My Code:
#include
#include
#include
#include
using namespace Std;
string converttostring (int num)
{
Ostringstream OS;
if (os< return os.str ();
Else
return "error";
}
int main ()
{
cout<< "Please input numbers:";
Vector VEC;
int number;
while (Cin>>number)
{
Vec.push_back (number);
}
String A, B;
Vector::iterator It=vec.begin ();
A=converttostring (*it);
For (It=vec.begin () +1;it!=vec.end (); it++)
{
B=converttostring (*it);
if ((a+b) > (b+a))
A=a+b;
Else
A=b+a;
}
cout< cout< return 0;
}
A piece of code from the online http://blog.csdn.net/yysdsyl/archive/2009/06/06/4248537.aspx:
#include
#include
#include
Class number
{
Public
Number (const char*s): STR (s) {}
BOOL operator < (const number&target) const
{
if (STR==TARGET.STR)
return false;
Std::string first=str+target.str;
Std::string second=target.str+str;
if (First>second)
return false;
else if (first return true;
Else
Return First.length ()};
Const CHAR*GETSTR () Const{return str.data ();};
Private
std::string str;
};
typedef std::set NUMBERS;
void Test (Number*target,int len)
{
NUMBERS numstrings;
for (int i=0;i numstrings.insert (target[i]);
For (Numbers::iterator It=numstrings.begin (); It!=numstrings.end (); ++it)
std::cout<< (*it). GETSTR () << '/t ';
Std::cout<}
int main ()
{
Number all3[]={"432", "4321", "43214"};
Number all[]={"123", "132", "213", "231", "321", "312"};
Number all1[]={"123123", "12312", "1231", "123", "12", "1"};
Number all2[]={"43214321", "4321432", "432143", "43214", "4321", "432"};
Number all4[]={"12", "3"};
#define TEST (T) test ((t), sizeof (t)/sizeof (number))
TEST (ALL3);
TEST (All);
TEST (ALL1);
TEST (ALL2);
TEST (ALL4);
System ("pause");
return 0;
}