2015 Huawei server test-weigh mice, 2015 Huawei weigh Mice
Description:
N white mice, each with a colored hat on their head. Now the weight of each white mouse is measured, and the color of the hats on the white mouse is required to be output in the order of size to size. The color of the hat is defined by the enumerated value MOUSE_COLOR. Different mice can wear hats of the same color. The weight of the white mouse is represented by an integer. Please output the color of the white mouse's hat in the ascending order of the weight of the white mouse. The color of the white mouse with the same weight remains unchanged after sorting.
Detailed description:
/* Output the hat color after sorting by weight
* Example: five mice with a weight of 15 30 5 9 30, respectively, and their hats are CL_RED, CL_BLUE, CL_BLUE, CL_YELLOW, CL_GRAY,
* The color sequence of the sorted hats should be CL_BLUE, CL_YELLOW, CL_RED, CL_BLUE, CL_GRAY.
If there is no mouse, the output is null.
Input parameter: Mouse Array
*/
Public static MOUSE_COLOR [] sortMouse (Mouse [] mouse)
{
Return null;
}
Example:
5 mice, respectively 15 30 5 9 30, respectively, and the hat colors are CL_RED, CL_BLUE, CL_BLUE, CL_YELLOW, CL_GRAY in sequence. Then, the color sequence of the sorted hats should be CL_BLUE, CL_YELLOW, CL_RED, CL_BLUE, CL_GRAY.
Solution: sort the weights of mice directly, and then obtain the color attribute of the mice.
The Code is as follows:
The rat giving the question
public class Mouse {public int weight;public MOUSE_COLOR color;public Mouse(){}public Mouse(int weight,MOUSE_COLOR color){this.weight =weight;this.color = color;}}
Enumeration type given by the question
public enum MOUSE_COLOR {CL_RED,CL_BLUE,CL_BLACK,CL_WHITE,CL_YELLOW,CL_PINK,CL_GRAY}
Solution code:
public class xiaobaishu{public static MOUSE_COLOR[] sortMouse(Mouse[] mouse ){MOUSE_COLOR[] cor=null;if(mouse==null||mouse.length==0){return null;}cor=new MOUSE_COLOR[mouse.length];for (int i = mouse.length-1; i >0; i--){for (int j = 0; j < i; j++){if (mouse[j].weight>mouse[j+1].weight){Mouse temp=mouse[j];mouse[j]=mouse[j+1];mouse[j+1]=temp;}}}for (int i = 0; i < mouse.length; i++){cor[i]=mouse[i].color;}return cor;}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.