2015 Huawei Machine Test--Xiao Ming's chopsticks

Source: Internet
Author: User
Tags ming

Title Description:


Xiao Ming is a careless, one day he went to the supermarket bought a number of chopsticks (n<20) The length of the chopsticks are not the same, he put all the chopsticks in the shopping bag to take home, the road accidentally missed a

Please use the program to help him find out how long the missing chopsticks are.


Input: Remaining chopsticks array, such as: 1, 2, 3, 2, 1, 3, 2


Return value: The length of the missing chopsticks, such as the input returned: 2 (when the input of the chopsticks data abnormal return-1, such as: Can not find missing chopsticks)



Problem-solving ideas: Using HashMap to the type of chopsticks and the number of key pairs of matching storage, when the repetition of chopsticks, the corresponding number of chopsticks +1, and then traverse a hashmap, to obtain the number of chopsticks,

If it is odd, it is missing chopsticks.


The code is as follows:


public class xiaoming_kuaizi{/** * @param args */public static void main (string[] args) {int[] chopsticks={1,2,3,2,1,3,2}; System.out.println (Checkchopsticks (chopsticks));}    /**     * Xiao Ming is a careless, one day he went to the supermarket to buy a number of chopsticks (n<20)     *  The length of the chopsticks are not the same, he put all the chopsticks in the shopping bag to take home, the road accidentally missed a root     *  Please use the program to help him find out how long the missing chopsticks are. * * * *      @param chopsticks  remaining chopsticks array, such as: 1, 2, 3, 2, 1, 3, 2     * @return int missing chopsticks length, as above input returned: 2 (returned when input chopsticks data is abnormal) -1, such as: Can not find missing chopsticks)     * *    /public static int checkchopsticks (int[] chopsticks)    {      Map<integer, Integer> hm=new hashmap<integer,integer> ();    int count=0;    for (int i = 0; i < chopsticks.length; i++) {if (Hm.containskey (Chopsticks[i])) {count=hm.get (chopsticks[i]) +1;} Else{count=1;} Hm.put (Chopsticks[i], count);}        for (int i = 0; i < chopsticks.length; i++) {if (Hm.get (Chopsticks[i])%2==0) {continue;} else if (Hm.get (Chopsticks[i])%2!=0) {return chopsticks[i];}}    return-1;}    }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

2015 Huawei Machine Test--Xiao Ming's chopsticks

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.