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
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=3589



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


Contents

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

Trivia

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.


Solution

import java.util.*;

public class Main{

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

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

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

    public static void solve(){
	String num1 = in.next();
	String num2 = in.next();
		
	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)
			counterA++;
		else
			counterB++;
		break;
	    }	
	}	
	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