Bull and cows

Source: Internet
Author: User

Package cn.edu.xidian.sselab.hashtable;

Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Set;

/**
*
* @author Zhiyong Wang
* Title:bull and cows
* Content:
* OU is playing the following Bulls and cows game with your friend:you write off a number and ask your friend to guess What's the number is. Each time your friend makes a guess, you provide a hint the indicates how many digits in said guess match your secret num ber exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the Wron G position (called "cows"). Your Friend would use successive guesses and hints to eventually derive the secret number.
* For example:
*
* Secret Number: "1807"
* Friend ' s guess: "7810"
*
* Hint:1 Bull and 3 cows. (The bull is 8, the cows is 0, 1 and 7.)
*
* Write a function to return a hint according to the secret number and friend's guess, use a to indicate the Bulls and B t o indicate the cows. In the above example, your function should return "1a3b".
*
* Please note that both secret number and friend's guess may contain duplicate digits, for example:
*
* Secret Number: "1123"
* Friend ' s guess: "0111"
*
The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return "1A1B".
*
* May assume the secret number and your friend's guess only contain digits, and their lengths is always equal.
*
*/
public class Bullandcows {

I did not think of it, refer to Daniel's problem-solving ideas
Here the default secret is equal to the length of guess
This place. The value of cow is to save 0 to 9 by creating a new array of 10 numbers, the initialization value is 0, if in secret, his corresponding value plus 1, if in guess, his corresponding value minus 1
This way, if the value of a bit in the secret is displayed in the array less than 0, it means that he has previously appeared in Guess, through guess.
public string Gethint (String secret, String guess) {
int[] numbers = new INT[10];
int bull = 0;
int cow = 0;
if (secret = = NULL | | guess = NULL) return null;
if (secret.length () = = 0 | | guess.length () = = 0)
return "0a0b";
for (int i=0;i<secret.length (); i++) {
if (Secret.charat (i) = = Guess.charat (i))
bull++;
else{
if (Numbers[secret.charat (i)-' 0 ']++ < 0) cow++;
if (Numbers[guess.charat (i)-' 0 ']--> 0) cow++;
}
}
return bull + "A" + Cow + "B";
}

}

?

Bull and cows

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.