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

前端 未结 16 2110
暖寄归人
暖寄归人 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
      #include
      #include
      using  namespace std;
    
      int main(){
      bool arranged=false;
      string arr[]={"98","12","56","9"};
      for(int i=0;i<4;i++)
        cout< 0 ) {
         swap(arr[i],arr[i-1]);
         arranged = false;
        }
       previous = arr[i];  
        }
     }
    
       for(int i=0;i<4;i++)
       cout<

提交回复
热议问题