首页 > 专题系列 > Java解POJ > POJ 3589 Number-guessing Game [解题报告] Java
2013
11-12

POJ 3589 Number-guessing Game [解题报告] Java

Number-guessing Game

问题描述 :

Larry likes playing the number-guessing game.

Two players are needed in a game. Suppose they are X and Y, and X presents a number for Y to guess. Firstly, X chooses a number with four different digits, keeping it in mind, and tells Y to start guessing. Every time Y has guessed, X should give out *A*B to show Y how close to the number his answer is. Here the symbol * stands for a number, and the number before A is the number of digits in Y's answer with both correct value and position. The number before B is the number of digits in Y's answer with correct value but incorrect position.

For example, if X chooses the number 5204, and Y guesses 4902, then X should give out 1A2B, in which 1A corresponds for digit 0 with both correct value and position and 2B corresponds for digit 2 and 4 with correct value but incorrect position. Then Y will go on guessing according to 1A2B that X presents him until he gets the totally correct number 5204 (when X shows him 4A0B).

Now you are given two numbers, and what you need to do is just testing how close they are.

输入:

The first line of the input is an integer T which indicates the number of test cases. For each test case, input two numbers. Each number contains four different digits.

输出:

For each test case, output *A*B stands for how close the two numbers are.
 

样例输入:

2
5204 4902
0123 3210

样例输出:

1A2B
0A4B

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;

	
public class Main {
 public static void main(String[] args)
 {
	Scanner in = new Scanner(System.in);
	int n = Integer.parseInt(in.nextLine());
	for(int j = 0; j< n; j++)
	{
		String first = in.next();
		String second = in.next();
		int res1 = 0;
		int res2 = 0;
		int[] done = new int[4];
		for(int i = 0; i< 4; i++)
		{
			if(first.charAt(i)==second.charAt(i))
			{	
				res1++;
				done[i]=1;
			}		
		}
		for(int i = 0; i< 4; i++)
		{
                for(int k = 0; k< 4; k++)
		   {
			if(first.charAt(i)==second.charAt(k))
			{
				if(done[i]==0)
					res2++;
			}
		}
	}
	System.out.println(res1+"A"+res2+"B");
  }
 }
}

  1. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish

  2. 老实说,这种方法就是穷举,复杂度是2^n,之所以能够AC是应为题目的测试数据有问题,要么数据量很小,要么能够得到k == t,否则即使n = 30,也要很久才能得出结果,本人亲测

  3. 为什么for循环找到的i一定是素数叻,而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak,而你每次取余都用的是原来的m,也就是n