Number-guessing Game

From Progteam

Revision as of 03:17, 13 September 2008 by MuGMaN (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Checkmark.jpg This problem has been solved by Eric.

Number-guessing Game
Problem Number 3589
Sorter: Eric
Source: South Central China 2008

Number-guessing Game is problem number 3589 on the Peking University ACM site.


Problem Information

Problem Name: Number-guessing Game
Problem Number on PKU: 3589
Synopsis: Two players are playing a number-guessing game in which Player B has to guess a 4-distinct-digit number and Player A has to tell Player B how close the guess is by giving information in the form of *A*B where * is an integer between 0 and 4. A represents the number of correct digits in the "right" place while B represents just the number of correct digits.

Solver Information

Solver: Eric Hong
Date: September 12, 2008


I accidentaly came across this question while I was looking for a problem to practice Ruby.

General Strategy

  1. Using Scanner, take the input.
  2. Using a nested for loop, compare each digit in numB to all digits in numA. (Note: numB is the guess and numA is the answer.)
  3. If two digits are equal:
    1. If two indices are equal, then increment counterA.
    2. Else, increment counterB.
  4. Else continue to next iteration.
  5. Display the results.


import java.util.*;

public class Main{

    public static Scanner in;
    public static void main(String[] args){
        in = new Scanner(;

    public static void doStuff(){
        int N = in.nextInt();

        for(int i = 0; i < N; i++){

    public static void solve(){
	String num1 =;
	String num2 =;
	compareDigits(num1, num2);
    public static void compareDigits(String num1, String num2){	
	int counterA = 0;
	int counterB = 0;
	for (int i = 0; i < 4; i++){
	    char ch2 = num2.charAt(i);
	    int digit2 = ch2 - '0';
	    for (int j = 0; j < 4; j++){
	        char ch1 = num1.charAt(j);
		if (ch2 == ch1){
		    if (i == j)
	System.out.println(counterA + "A" + counterB + "B");

Additional Remarks

Memory: 2136K
Time: 188MS

This solution may not be as efficient as it can be.

Personal tools