2015 Huawei server test-weigh mice, 2015 Huawei weigh Mice

Source: Internet
Author: User

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:

  • Interface 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.

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.