"Greedy algorithm" maximum integer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.