Analysis of the largest number of Structures

Source: Internet
Author: User

Given an array containing only positive numbers, a method is provided to concatenate the numbers in the array. The maximum number is obtained. For example, after splicing [4, 94, 9, 14, 1], the maximum number obtained is 9944141.

Analysis

We can compare two numbers as a whole. Then, the result is obtained after sorting. Example: 5, 54, 56

Compared to 5 and 54, it is actually larger than 545 and 554.

Compared to 5 and 56, it is actually larger than 556 and 565.

Compared to 54 and 56, it is actually larger than 5456 and 5654.

Then let's make a change to the fast-paced program. When we compare two numbers A and B, we can compare the sizes of the numbers AB and Ba. However, the comparison has changed, and the rest is the same as that of the fast sort. To prevent overflow, we put two numbers in a string for comparison. The Code is as follows:

Char num1 [30], num2 [30]; bool CMP (INT value1, int value2) {char a [15], B [15]; sprintf (, "% d", value1); sprintf (B, "% d", value2); strcpy (num1, ); // concatenate two numbers into an array to compare strcat (num1, B); strcpy (num2, B); strcat (num2, a); int res = strcmp (num1, num2); If (RES> = 0) return true; return false;} string makebignum (vector <int> & Data) {int length = data. size (), I; If (length <= 0) return NULL; sort (data. begin (), Data. end (), CMP); // sort string res = ""; for (I = 0; I <length; I ++) {stringstream SS; SS <data [I]; Res + = ss. STR ();} return res ;}




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.