UVa 340 Master-Mind Hints(猜数字游戏的提示)
题意 猜数字游戏 统计猜的数字有多少个数字位置正确 有多少个数字在答案中出现可是位置不对 每一个字符仅仅能匹配一次
直接匹配每位数
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
const int N = 1005;
int a[N], b[N], c, d;
int main()
{
int n, cas = 0;
while (scanf ("%d", &n), n)
{
map<int, int> aa;
printf ("Game %d:\n", ++cas);
for (int i = 0; i < n; ++i)
{
scanf ("%d", &a[i]);
++aa[a[i]];
}
while (1)
{
map<int, int> bb;
for (int i = c = d = 0; i < n; ++i)
{
scanf ("%d", &b[i]);
++bb[b[i]];
if (a[i] == b[i]) c++;
}
if (!b[0]) break;
for (int i = 1; i <= 9; ++i)
d += min (aa[i], bb[i]);
printf (" (%d,%d)\n", c, d - c);
}
}
return 0;
}
| 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
and a guess
,
and are to determine the hint. A hint consists of a pair of numbers determined as follows.
A match is a pair (i,j),
and
,
such that
. Match (i,j) is called strongwhen i = j,
and is called weak otherwise. Two matches (i,j) and (p,q) are called independent wheni = 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 zero (when a value forN 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.
Sample Input
4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
0
Sample Output
Game 1:
(1,1)
(2,0)
(1,2)
(1,2)
(4,0)
Game 2:
(2,4)
(3,2)
(5,0)
(7,0)
UVa 340 Master-Mind Hints(猜数字游戏的提示)的更多相关文章
- [C++]猜数字游戏的提示(Master-Mind Hints,UVa340)
[本博文非博主原创,思路与题目均摘自 刘汝佳<算法竞赛与入门经典(第2版)>] Question 例题3-4 猜数字游戏的提示(Master-Mind Hints,UVa340) 实现一个 ...
- 猜数字游戏的提示(Master-Mind Hints, UVa 340)
实现一个经典"猜数字"游戏. 给定答案序列和用户猜的序列,统计有多少数字位置正确 (A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 ...
- 例题3_4 猜数字游戏的提示(UVa340)
实现一个经典“猜数字”游戏.给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度n,第二行是答案序列,接 ...
- 猜数字游戏的提示(UVa340)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- C语言猜数字游戏
猜数字游戏,各式各样的实现方式,我这边提供一个实现方式,希望可以帮到新手. 老程序猿就不要看了,黑呵呵 源代码1 include stdio.h include stdlib.h include ti ...
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- java 猜数字游戏
作用:猜数字游戏.随机产生1个数字(1~10),大了.小了或者成功后给出提示. 语言:java 工具:eclipse 作者:潇洒鸿图 时间:2016.11.10 >>>>> ...
- 【原创Android游戏】--猜数字游戏Version 0.1
想当年高中时经常和小伙伴在纸上或者黑板上或者学习机上玩猜数字的游戏,在当年那个手机等娱乐设备在我们那还不是很普遍的时候是很好的一个消遣的游戏,去年的时候便写了一个Android版的猜数字游戏,只是当时 ...
- 【原创Android游戏】--猜数字游戏V1.1 --数据存储,Intent,SimpleAdapter的学习与应用
--------------------------------------------------------------- V0.1版本 上次做完第一个版本后,发现还有一些漏洞,并且还有一些可以添 ...
随机推荐
- Xcode 9 打印信息解决
Xcode 9 打印信息解决 打印信息 1 nw_proxy_resolver_create_parsed_array PAC evaluation error: kCFErrorDomainCFNe ...
- js阻塞ui进程涉及的知识点整理
项目进行中遇到了同步ajax阻塞ui线程阻塞的问题,原因是执行两个同步ajax请求为一次完整的方法,因业务需求需要循环执行这个方法,检查后台返回的数据正确,但是由于ajax请求时间过长,考虑增加遮罩层 ...
- scrapy增加爬取效率
增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100. 降低日志级别 ...
- tomcat添加访问的ip限制
在如下位置添加如下代码: 代码: <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow=&q ...
- oracle分析函数之ratio_to_report
ratio_to_report主要完成对百分比的计算,语法为ratio_to_report(exp) over()也就是根据over窗口函数的作用区间,求出作用区间中的单个值在整个区间的总值的比重比如 ...
- x264介绍
x264 编辑 H.264是ITU(International Telecommunication Unite 国际通信联盟)和MPEG(Motion Picture Experts Group ...
- Class加载顺序
原文:https://blog.saymagic.cn/2017/07/01/class-common-question.html 类的初始化顺序是怎样的? 我们尝试从class文件中找到答案.来看这 ...
- Java权限管理(授权与认证)
CRM权限管理 有兴趣的同学也可以阅读我最近分享的:Shiro框架原理分析 (PS : 这篇博客里面介绍了使用Shiro框架的方式实现权限管理) https://www.cnblogs.com/y ...
- [Python3网络爬虫开发实战] 5.2-关系型数据库存储
关系型数据库是基于关系模型的数据库,而关系模型是通过二维表来保存的,所以它的存储方式就是行列组成的表,每一列是一个字段,每一行是一条记录.表可以看作某个实体的集合,而实体之间存在联系,这就需要表与表之 ...
- 微信小程序 video组件----真机测试position:fixed无效 且有黑底
1.问题描述 video组件fixed后,视频随页面滚动,且有个黑色底停留在页面. 页面滚动前 滚动后 这里贴一下修改前代码,在微信开发者工具看是没有任何问题的.在手机端测试就有以上的问题 <v ...