PAT (Advanced Level) Practice - 1148 Werewolf - Simple Version(20 分)

简介: PAT (Advanced Level) Practice - 1148 Werewolf - Simple Version(20 分)

题目链接:点击打开链接

题目大意:已知 N 名玩家中有 2 人扮演狼人角色,有 2 人说的不是实话,有狼人撒谎但并不是所有狼人都在撒谎(即:说谎者==1只人类 + 1只狼人)。要求你找出扮演狼人角色的是哪几号玩家,如果有解,在一行中按递增顺序输出 2 个狼人的编号;如果解不唯一,则输出最小序列解;若无解则输出 No Solution~

解题思路:每个人说的数字保存在v数组中,i从1~n、j从i+1~n遍历,分别假设i和j是狼人,a数组表示该人是狼人还是好人,等于1表示是好人,等于-1表示是狼人。k从1~n分别判断k所说的话是真是假,k说的话和真实情况不同(即v[k] * a[abs(v[k])] < 0)则表示k在说谎,则将k放在lie数组中;遍历完成后判断lie数组,如果说谎人数等于2并且这两个说谎的人一个是好人一个是狼人(即a[lie[0]] + a[lie[1]] == 0)表示满足题意,此时输出i和j并return,否则最后的时候输出No Solution~


AC 代码

#include<bits/stdc++.h>#include<cmath>#define mem(a,b) memset(a,b,sizeof a)#define ssclr(ss) ss.clear(), ss.str("")#define INF 0x3f3f3f3f#define MOD 1000000007usingnamespacestd;
typedeflonglongll;
intmain()
{
intn,b;
scanf("%d",&n);
vector<int>v(n+1);
for(inti=1;i<=n;i++) scanf("%d",&b), v[i]=b;
for(inti=1;i<=n;i++)
    {
for(intj=i+1;j<=n;j++)
        {
vector<int>a(n+1,1), lie;
a[i]=a[j]=-1; // 狼人for(intk=1;k<=n;k++)
if(v[k]*a[abs(v[k])]<0) lie.push_back(k);
if(lie.size()==2&&a[lie[0]]+a[lie[1]]==0)
            {
printf("%d %d\n",i,j);
return0;
            }
        }
    }
puts("No Solution");
return0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
96 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
131 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
120 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
140 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
141 0
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
126 0
PAT (Advanced Level) Practice - 1146 Topological Order(25 分)
PAT (Advanced Level) Practice - 1146 Topological Order(25 分)
87 0
PAT (Advanced Level) Practice - 1130 Infix Expression(25 分)
PAT (Advanced Level) Practice - 1130 Infix Expression(25 分)
127 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
117 0
PAT (Advanced Level) Practice - 1018 Public Bike Management(30 分)
PAT (Advanced Level) Practice - 1018 Public Bike Management(30 分)
194 0
下一篇
无影云桌面