博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【UVA】 340 --- Master-Mind Hints
阅读量:2039 次
发布时间:2019-04-28

本文共 3595 字,大约阅读时间需要 11 分钟。

【UVA】 340 --- Master-Mind Hints

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker,
tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players
agree upon the length N that a code must have and upon the colors that may occur in a code.
In order to break the code, Breaker makes a number of guesses, each guess itself being a code. After
each guess Designer gives a hint, stating to what extent the guess matches his secret code.
In this problem you will be given a secret code s1 . . . sn and a guess g1 . . . gn, and are to determine
the hint. A hint consists of a pair of numbers determined as follows.
A match is a pair (i, j), 1 ≤ i ≤ n and 1 ≤ j ≤ n, such that si = gj . Match (i, j) is called strong
when i = j, and is called weak otherwise. Two matches (i, j) and (p, q) are called independent when
i = p if and only if j = q. A set of matches is called independent when all of its members are pairwise
independent.
Designer chooses an independent set M of matches for which the total number of matches and the
number of strong matches are both maximal. The hint then consists of the number of strong followed
by the number of weak matches in M. Note that these numbers are uniquely determined by the secret
code and the guess. If the hint turns out to be (n, 0), then the guess is identical to the secret code.

Input

The input will consist of data for a number of games. The input for each game begins with an integer
specifying N (the length of the code). Following these will be the secret code, represented as N integers,
which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, each
also represented as N integers, each in the range 1 to 9. Following the last guess in each game will be
N zeroes; these zeroes are not to be considered as a guess.
Following the data for the first game will appear data for the second game (if any) beginning with a
new value for N. The last game in the input will be followed by a single ‘0’ (when a value for N would
normally be specified). The maximum value for N will be 1000.

Output

The output for each game should list the hints that would be generated for each guess, in order, one hint
per line. Each hint should be represented as a pair of integers enclosed in parentheses and separated by
a comma. The entire list of hints for each game should be prefixed by a heading indicating the game
number; games are numbered sequentially starting with 1. Look at the samples below for the exact
format.
思路:
用两个数组分别装答案序列和用户猜的序列。
记录相同位置且数相同的个数x;
遍历1~9,统计二者出现的次数h1和h2,y+=min(h1,h2)得到总的重复个数。
y-x即:数字在两个序列都出现过但位置不对的个数。

#include 
using namespace std;int arr1[1001], arr2[1001], num = 1;int main(){
int n; while (cin >> n) {
if (n == 0) {
break; } for (int i = 0; i < n; i++) {
cin >> arr1[i]; // 输入答案序列 } printf("Game %d:\n", num++); while (1) {
int x = 0, y = 0; for (int i = 0; i < n; i++) {
cin >> arr2[i]; // 输入用户猜的序列 if (arr1[i] == arr2[i]) // 记录相同位置相同数的个数. {
x++; } } if (arr2[0] == 0) // 一定要输入完成后在判断 如果输入一个0就退出循环了 那后面输入的数会留在缓冲区影响下次循环 {
break; } for (int i = 1; i <= 9; i++) // 用h1,h2记录数字为i分别在两个序列中的个数 {
int h1 = 0, h2 = 0; for (int j = 0; j < n; j++) {
if (arr2[j] == i) {
h2++; } if (arr1[j] == i) {
h1++; } } y += h1 > h2 ? h2 : h1; // 用y记录数字为i的两个序列中最少的个数,方便y-x计算数字在两个序列都出现过但位置不对的个数。 } cout << " (" << x << "," << y - x << ")" << endl; } } return 0;}

转载地址:http://lgyof.baihongyu.com/

你可能感兴趣的文章
java中的BigDecimal和String的相互转换
查看>>
hadoop、storm和spark的区别、比较
查看>>
Tomcat调优总结
查看>>
hibernate hql 同时更新多个字段
查看>>
oracle键、索引、约束及其区别
查看>>
解决activemq多消费者并发处理
查看>>
网络通信分享(一):数字签名,数字证书,https通信,数据加密
查看>>
关于Class.getResource和ClassLoader.getResource的路径问题
查看>>
Spring MVC 基于session 国际化配置!! 亲测可用
查看>>
Netty维持长连接 消息推送及心跳机制
查看>>
如果有人问你数据库的原理,叫他看这篇文章
查看>>
Java 应用一般架构
查看>>
java中的本地缓存
查看>>
一些不错技术博客列表 长期更新~~
查看>>
UDP连接和TCP连接的异同
查看>>
hibernate 时间段查询
查看>>
《深入浅出 Java Concurrency》目录
查看>>
java操作cookie 实现两周内自动登录
查看>>
在Cookie被禁用的情况下使用url rewrite机制保持Session
查看>>
Java根据sessionId获取Session对象
查看>>