PAT 1148 Werewolf - Simple Version [难理解]
1148 Werewolf - Simple Version (20 分)
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game,
- player #1 said: "Player #2 is a werewolf.";
- player #2 said: "Player #3 is a human.";
- player #3 said: "Player #4 is a werewolf.";
- player #4 said: "Player #5 is a human."; and
- player #5 said: "Player #4 is a human.".
Given that there were 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. Can you point out the werewolves?
Now you are asked to solve a harder version of this problem: given that there were N players, with 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. You are supposed to point out the werewolves.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (5≤N≤100). Then N lines follow and the i-th line gives the statement of the i-th player (1≤i≤N), which is represented by the index of the player with a positive sign for a human and a negative sign for a werewolf.
Output Specification:
If a solution exists, print in a line in ascending order the indices of the two werewolves. The numbers must be separated by exactly one space with no extra spaces at the beginning or the end of the line. If there are more than one solution, you must output the smallest solution sequence -- that is, for two sequences A=a[1],...,a[M] and B=b[1],...,b[M], if there exists 0≤k<M such that a[i]=b[i] (i≤k) and a[k+1]<b[k+1], then A is said to be smaller than B. In case there is no solution, simply print No Solution.
Sample Input 1:
5
-2
+3
-4
+5
+4
Sample Output 1:
1 4
Sample Input 2:
6
+6
+3
+1
-5
-2
+4
Sample Output 2 (the solution is not unique):
1 5
Sample Input 3:
5
-2
-3
-4
-5
-1
Sample Output 3:
No Solution
题目大意:狼人杀里有狼人和村民,假设在一群人中一共有两个狼人,并且至少有一个狼人但并不是所有的狼人都说慌,而且恰好有两个说谎的人,那么你需要找出狼人。如果解不唯一,输出序号小的一组。
//当时考试的时候看着道题目,完全不理解,看了20分钟就放弃了,也是通过率最低的一道题目
//看柳神的题解,说是水题,我奔溃了。
代码来自:https://www.liuchuo.net/archives/6494
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n+);
for (int i = ; i <= n; i++) cin >> v[i]; for (int i = ; i <= n; i++) {
for (int j = i + ; j <= n; j++) {
vector<int> lie, a(n + , );//都赋值为1.
a[i] = a[j] = -;//如果这两者是狼人。
for (int k = ; k <= n; k++)
if (v[k] * a[abs(v[k])] < ) lie.push_back(k);
//<0,则表示K在说谎。
if (lie.size() == && a[lie[]] + a[lie[]] == ) {
cout << i << " " << j;//两者都不是狼人。
return ;//直接返回,就是最小的标号
}
}
}
cout << "No Solution";
return ;
}
//这个判断<0,简直不要太厉害了。
1.v[i]表示i说v[i]是什么;
2.使用符号来判断是否说谎;
3.a[abs(v[k])]的符号用来判断当前假定条件下(a[i]和a[j]是狼人),v[k]是当前第k个人说v[k]是村民还是狼人,符号来表示,如果两者不一致,那么在此种假设下,就是说谎;
4.对于说谎的,如果长度正好为2,并且一个是狼人一个是村民,也就是二者的和为0,正负抵消。
PAT 1148 Werewolf - Simple Version [难理解]的更多相关文章
- PAT 1148 Werewolf - Simple Version
1148 Werewolf - Simple Version (20 分) Werewolf(狼人杀) is a game in which the players are partitioned ...
- PAT(A) 1148 Werewolf - Simple Version(Java)逻辑推理
题目链接:1148 Werewolf - Simple Version (20 point(s)) Description Werewolf(狼人杀) is a game in which the p ...
- PAT A1148 Werewolf - Simple Version (20 分)——暴力遍历,负负得正
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...
- 1148 Werewolf - Simple Version (20 分)
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...
- 1148 Werewolf - Simple Version
Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and th ...
- PAT_A1148#Werewolf - Simple Version
Source: PAT 1148 Werewolf - Simple Version (20 分) Description: Werewolf(狼人杀) is a game in which the ...
- K8S核心概念之SVC(易混淆难理解知识点总结)
本文将结合实际工作当中遇到的一些问题和情况来解析SVC的作用以及一些比较易混淆和难理解的概念,方便日后工作用到或者遗忘时可以直接在自己曾经学习总结的博客当中直接查找到. 首先应该清楚SVC的作用是什么 ...
- PAT 1056 Mice and Rice[难][不理解]
1056 Mice and Rice(25 分) Mice and Rice is the name of a programming contest in which each programmer ...
- 认为C/C++很难理解、找工作面试笔试,快看看这本书!
假设你是C/C++谁刚开始学习,看这本书.因为也许你读其他的书还不如不看.一定要选择一本好书. 假设你正在准备工作,请认真看这本书,由于这本书会教会你工作中必备的知识,相信你即将面临的语法类题目不会超 ...
随机推荐
- PHP 汉字转成拼音
<?php class ZH{ /** * 将字符串转化为拼音 */ function Pinyin($_String, $_Code='gb2312') { $_DataKey =" ...
- CI 结合Bootstrap 分页
#显示列表 public function index(){ //载入分页类 $this->load->library('pagination'); //每页显示的条数 $perPage= ...
- 【BZOJ】1647: [Usaco2007 Open]Fliptile 翻格子游戏(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1647 自己太弱...看题解.. 竟然是枚举第一行的放法,,,因为一定要全部变0,所以将前一行1的在这 ...
- 怎么用ChemDraw连接两个结构片段
作为最新版的ChemOffice 15.1的核心组件,ChemDraw几乎能解决所有平面化学结构中的绘制问题.如果用户想连接两个分开的结构片段,ChemDraw提供两种连接两个化学结构片段的方法,分别 ...
- 关于 AfxSocketInit()
一般来说 WASAtarup() 是应用程序调用的Windows Sockets dll的第一个函数,在调用任何Winsock Api之前,必须调用WSAStartup()进行初始化,最后调用WSAC ...
- CStringArray序列化处理
开发中需要对CStringArray进行保存操作,涉及到序列化,特总结一下: //写 CStringArray saTmp1; CStringArray saTmp2 saTmp1.AddString ...
- Struts2_day03--从值栈获取数据_EL表达式获取值栈数据(为什么)
从值栈获取数据 1 使用struts2的标签+ognl表达式获取值栈数据 (1)<s:property value=”ognl表达式”/> 获取字符串 1 向值栈放字符串 2 在jsp使用 ...
- Hibernate_day02--Hibernate的一级缓存
Hibernate的一级缓存 什么是缓存 1 数据存到数据库里面,数据库本身是文件系统,使用流方式操作文件效率不是很高. (1)把数据存到内存里面,不需要使用流方式,可以直接读取内存中数据 (2)把数 ...
- 剑指 offer set 24 扑克牌的顺子
题目 从扑克牌中任意抽取出 5 张牌, 判断是不是顺子, 并且大小王可以看成任意的数字 思路 1. 把大小王当做 0 插入到数组中, 然后对数组排序 2. 统计相邻两个数之间的空隙数, 若空隙数大于 ...
- swift开发之--Protocol(协议)
使用object-c语言的同学们肯定对协议都不陌生,但在swift中苹果将protocol这种语法发扬的更加深入和彻底. Swift中的protocol不仅能定义方法还能定义属性,配合extensio ...