Number Guessing

本文介绍了一种猜数字游戏的算法实现,玩家需根据计算机反馈调整猜测,最终确定由四个不同数字组成的答案。文章详细解释了游戏规则及输入输出要求,并提供了一个具体的C++实现示例。

Number Guessing

时间限制(普通/Java):3000MS/10000MS          运行内存限制:65536KByte
总提交:38            测试通过:16

描述

Number Guessing is a computer game. First, the computer chooses four different digits, you need to guess these four digits in the fewest times,for each guess, the computer will show a judgement in the form of "#A#B", "#" is a number 0~4. "#A" shows how many digits you guessed with both correct value and position. "#B" shows how many digits you guessed with correct value. For example, the computer chose 1234, and you guessed 6139, the computer will show "1A2B" for you have number "1" correct value but wrong position and number "3" correct value with correct position.Thus the computer gives you the judgement of "1A2B". Now you have memorized the digits you guessed and the judgements you got, you feel like you can figure out the correct answer. Life is filled with wisdom, isn't it?

输入

There are several test cases. For each test case, the first line contains a single positive integer N indicates the times you can guess,the following N lines is the record of the guess, in the form:

#### #A#B

 

The first four numbers is the numbers guessed,then the judgements for your guess.The input is terminated by a negative integer.

 

输出

For each test case, output a single line contains exactly four digits that the computer has chosen. You may assume that each test case gives you enough information, so you can figure out the unique correct answer.

样例输入

2
1234 2A4B
1243 0A4B
3
0732 3A3B
1526 0A0B
4567 0A2B
-1

样例输出

2134
0734

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<cstdlib>

#define NUMBER_MAX_LENGTH 4
#define RESULT_MAX_LENGTH 4
#define CASE_MAX_NUMBER 1000

class Case{
public:
	void set(void){ scanf("%s%s", number, result); }
	bool isFitThisNumber(const char* n);
private:
	char number[NUMBER_MAX_LENGTH + 1];
	char result[RESULT_MAX_LENGTH + 1];
};

bool Case::isFitThisNumber(const char* bit){
	char posRight = '0';
	char posValueRight = '0';
	for (size_t ix = 0; ix != NUMBER_MAX_LENGTH; ++ix){
		if (number[ix] == bit[ix]){ ++posValueRight; }
	}
	for (size_t i = 0; i != NUMBER_MAX_LENGTH; ++i){
		for (size_t j = 0; j != NUMBER_MAX_LENGTH; ++j){
			if (number[i] == bit[j]){
				++posRight;
				break;
			}
		}
	}
	return posRight == result[2] && posValueRight == result[0];
}

Case numberCase[CASE_MAX_NUMBER];
int caseSize;

bool isFitAllCase(const int n){
	char bit[NUMBER_MAX_LENGTH] = { n / 1000 + '0', n / 100 % 10 + '0', n / 10 % 10 + '0', n % 10 + '0' };
	if (n / 1000 > 0 && (bit[0] == bit[1] || bit[0] == bit[2] || bit[0] == bit[3] || bit[1] == bit[2] || bit[1] == bit[3] || bit[2] == bit[3])){
		return false;
	}
	else if (n / 100 > 0 && bit[1] == bit[2] || bit[1] == bit[3] || bit[2] == bit[3]){
		return false;
	}
	else if (n / 10 > 0 && bit[2] == bit[3]){
		return false;
	}
	else{
		for (size_t ix = 0; ix != caseSize; ++ix){
			if (!numberCase[ix].isFitThisNumber(bit)){ return false; }
		}
		return true;
	}
}

void setCase(void){
	for (size_t ix = 0; ix != caseSize; ++ix){
		numberCase[ix].set();
	}
}

int main(void){
	while (cin >> caseSize, caseSize >= 0){
		setCase();
		for (int i = 0; i != 10000; ++i){
			if (isFitAllCase(i)){
				printf("%04d\n", i);
				break;
			}
		}
	}
    return EXIT_SUCCESS;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值