How can I manipulate an array to make the largest number?

前端 未结 16 2107
暖寄归人
暖寄归人 2021-01-30 02:47

Say you have an array of positive integers, manipulate them so that the concatenation of the integers of the resultant array is the largest number possible. Ex: {9,1,95,17,5}, r

相关标签:
16条回答
  • 2021-01-30 03:20

    Okay, how about this algorithm , which uses compare function to check previous number and the number in the next index.For the sake of simplicity, i have used strings instead of integers.Though, the algorithm well explains what it is doing.

      #include<string>
      #include<iostream>
      #include<algorithm>
      using  namespace std;
    
      int main(){
      bool arranged=false;
      string arr[]={"98","12","56","9"};
      for(int i=0;i<4;i++)
        cout<<arr[i]<<" ";
        cout<<endl;
    
    while( arranged==false )
    { 
    string previous = arr[0];
      arranged = true;
      for(int i = 1; i < 4;i++)
      { 
        string  XY = (previous + arr[i] ); 
        string  YX = (arr[i] + previous);        
          if ( YX.compare(XY) > 0 ) {
         swap(arr[i],arr[i-1]);
         arranged = false;
        }
       previous = arr[i];  
        }
     }
    
       for(int i=0;i<4;i++)
       cout<<arr[i];
    
       return 0;
     }  
    
    0 讨论(0)
  • 2021-01-30 03:21

    I would use the following function to sort them

    class Kakira {
    
        static int preferred(int a, int b) {
            if(a == b) return a; // doesn't matter which
            String sa = a+"";
            String sb = b+"";
    
            for(int i = 0; i < sa.length() && i < sb.length(); i++) {
                char ca = sa.charAt(i);
                char cb = sb.charAt(i);
                if(ca < cb) return b;
                if(ca > cb) return a;
            }
            // we reached here - the larger one must start with the smaller one
            // so, remove the small one from the start of the small one, and
            // that will tell us which is most appropriate.
            if(a < b) {
                String choppedB = sb.substring(sa.length());
                if(preferred(Integer.parseInt(choppedB),a) == a) 
                    return a;
                else
                    return b;
            }
            else {
                String choppedA = sa.substring(sb.length());
                if(preferred(Integer.parseInt(choppedA),b) == b) 
                    return b;
                else
                    return a;
            }
        }
    
        // using a very simple sort because I'm being lazy right now
        public static void sort(int[] data) {
            while(!isSorted(data)) {
                for(int i = 0; i < data.length - 1; i++) {
                    int a = data[i];
                    int b = data[i+1];
                    int p = preferred(a,b);
                    if(p == b) {
                        data[i] = b;
                        data[i+1] = a;
                    }
                }
            }
        }
    
        public static boolean isSorted(int[] data) {
            for(int i = 0; i < data.length - 1; i++) {
                int a = data[i];
                int b = data[i+1];
                int p = preferred(a,b);
                if(p != a) return false;
            }
            return true;
        }
    
        public static void main(String[] args) {
            int[] data = new int[]{9,1,95,17,5};
            sort(data);
            for(int i : data) System.out.print(i);
            System.out.println("");
        }
    
    }
    

    For the humans: First: I'm going to sort them using a special comparison function. This will put them in the most desired order, so we can just print them out.

    I'm not worrying about the sort algorithm, because once you have the comparison you can use whatever sorting you want. The comparison is the important part.

    So let's walk through the decision process.

    First, if the two numbers are equal, no one cares which one it is. So we just return it.

    Next, we get the string representation of the numbers so we can start comparing digits. We walk down the digits until we run out of digits on one of the strings. Once they are no longer equal, we want the one with the larger value, because we want high digits early (I think it's obvious why).

    If we get to the end of one of the numbers and we don't yet have a winner, we know the longer one must start with the short one. They CAN'T be of equal length, because if the first digits are equal, and they're equal length, then the numbers themselves are equal, and they would have been caught earlier in the process. So the question is, which one goes first? Well, consider the ramifications:

    12345 first: 12345123
    123 first: 12312345
    

    Obviously we want the first one. But how to know this? We take the 123 away from the long one. So we have 45 and 123.

    Now, run these through the algorithm again, determine which was the winner, and whichever won the second round also wins the first round. If it were to go deeper, say (12312312 and 123) then it would continue to chain down.

    Make sense?

    0 讨论(0)
  • 2021-01-30 03:22

    Not sure if anyone is looking for a JavaScript solution, but if you are, here is the code

    function LexicographicSort(input) {
        return Number(
            input
                .sort(function(a, b) {
    // lexicographic sort
                   return Number("" + b + a) - Number("" + a + b);
                })
                .join("")
            );
    }
    
    0 讨论(0)
  • 2021-01-30 03:23

    Here is Python 3 solution where a is an array and n is the total number of element

    def check(p,q):
        p=str(p)
        q=str(q)
        d=max(p+q,q+p)
        if d==p+q:
            return int(p),int(q)
        else:
            return int(q),int(p)
    def find(a,n):
        for i in range(n):
            for j in range(n-1):
                c,d=check(a[j],a[j+1])
                a[j]=c
                a[j+1]=d
        print(*a,sep="")
    
    0 讨论(0)
提交回复
热议问题