Home > Back-end >  Huawei OD machine exam _ given an array, the joining together into a maximum
Huawei OD machine exam _ given an array, the joining together into a maximum

Time:10-13

Try twice this year to participate in the huawei OD machine, sorry didn't pass, try machine of topic to share with everyone here, hope everybody progresses together, huawei machine test subject description is more complex, meaning about the following
Title:
Given an array, the inside of the each Numbers are not more than 5, the maximum please joining together, after mosaics of the array length of 25, the maximum


Public class MaximumNumber {

/*
Given an array: combined into the biggest value
*/
Public String combinate (int [] nums) {
If (null==nums | | nums. Length==0) {
return "";
}
The StringBuilder StringBuilder=new StringBuilder ();
CompareNums (nums);
for (int i=0; I & lt; Nums. Length; I++) {
The stringBuilder. Append (String. The valueOf (nums [I]));
}
Return the stringBuilder. ToString ();
}

/*
Sorting
*/
Private void compareNums (int [] nums) {
If (null==nums) {
Throw new NullPointerException ();
}
If (nums. Length==1) {
return;
}
int temp;
for (int i=0; I & lt; Nums. Length - 1; I++) {
For (int j=I + 1; J & lt; Nums. Length; J++) {
If (compare (nums [I], nums [j]]) {
Temp=nums [I];
Nums=nums [I] [j];
[j] nums=temp;
}
}
}

}

/*
Compare the two Numbers which make high order
Example: 9, 98 - & gt; 998; 15, 18 - & gt; 1815
*/
Private Boolean compare (int a, int b) {
Aa=String String. The valueOf (a);
String bb=String. The valueOf (b);
Int ab=Integer. The valueOf (aa + bb);
Int ba=Integer. The valueOf (bb + aa);
If (ba & gt; Ab) {
return true;
} else {
return false;
}
}

Public static void main (String [] args) {
MaximumNumber MaximumNumber=new MaximumNumber ();
Int [] b={35, 11, 23, 4, 34};
System. The out. Println (maximumNumber.com binate (b));
}
}

  • Related