简体   繁体   中英

5 digit number game algorithm

Hi I need an algorithm for solving this problem.Host thinks of a number 98321 and Guess is 12345. Host would respond with ABBCC. 'A' is for 3(correct and middle),'BB' is for 12 correct(but in wrong position) and the CC is for 45(incorrect digits).

This is clearly homework, and I won't give you the plain solution for it. However, this exercise game is popular enough for you to find the solution by googling enough.

Anyway, here is some pseudo-code that can help you:

guess_game(s1, s2):
    toret = ""

    for i <- 0 to lengthOf( s1 ) - 1:
        is s1[i] == s2[ i ] ?:
            yes: toret += "A"
            no: is s2[ i ] in s1 ?:
                yes: toret += "B"
                no: toret += "C"

    return toret

Hope this inspires you enough to take the challenge and get into programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM