C + + Arrays are ranked the largest number (arrays, algorithms, permutations)

Source: Internet
Author: User

Rows the array into the largest number (array, algorithm). Title: Enter an array of positive integers, concatenate them into a number, and output the largest of all the numbers that can be drained. For example, the input array {32, 321}, the output of the two can be ranked the maximum number 32321: or enter the array {10,9,33,1000} to output the four largest numbers 933101000 that can be arranged. #include <iostream> #define MAXSIZE 100using namespace std;  Template<typename t>class grial{public:int Length (int x,int& n) {n++; (X/10) && (Length (x/=10,n)); return n;} The number of digits to be evaluated. BOOL Complate (int x,int y)//Compare the size of x, Y, is the size of the combination arrangement, see XY than the size of YX, so as to form a large heap. {int _i = 0;int I = 0;int j = 0;int Sum_1=x;int sum_2=y;    Length (X,i); Length (Y,J); for (; _i<j;_i++) {sum_1*=10;} for (_i=0;_i<i;_i++) {sum_2*=10;} if ((sum_1+y) > (sum_2+x)) return True;elsereturn false;} Grial (T a[],int n) {data = new t[n];size = n;for (int i=0;i<n;i++) {Data[i] = a[i];} int m = Size/2;while (m<size) {maxsort (m); m++;}} void Maxsort (int n)//Large heap arrangement {int temp;int i=n;int j= (i-1)/2;while (i>0) {if (! Complate (Data[j],data[i])) {temp = Data[i];d ata[i]=data[j];d ata[j]=temp;} i = J;j = (i-1)/2;}} int GetTop ()//Gets the first element of the heap, and puts the last element in the position labeled 0, and then makes a large heap of sorts. {if (size==0) Return-1;int temp =Data[0];d ata[0] = data[size-1];size--;int m = size/2;while (m<size) {maxsort (m); m++;} return temp;}  void Getsum () {int i = GetTop (); if (i!=-1) {cout<<i; Getsum ();}} Output data. private:t *data;int size;}; int main () {int a[]={1,9,100001,1001111,98}; Grial<int> G (a,5);//The result is 99811001111100001 to meet the maximum permutation combination.  G.getsum (); Cout<<endl;return 0;}

C + + Arrays are ranked the largest number (arrays, algorithms, permutations)

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.