仓鼠的石子游戏

mac2026-08-23  1

仓鼠的石子游戏

题解

我们先看数量大于2的环,那么一定是先放入这个环的人输。所以只有个数为1的环会对答案产生影响,我们只需统计个数为1的环的数量。再判断它的奇偶性即可。

源码

#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<stack> #include<vector> #include<queue> using namespace std; typedef long long LL; const LL INF=0x3f3f3f3f; int t,n; #define gc() getchar() template<typename _T> inline void read(_T &x) { _T f=1;x=0;char s=gc(); while(s>'9'||s<'0'){if(s=='-')f=-1;s=gc();} while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=gc();} x*=f; } int main() { read(t); while(t--) { read(n);int res=0; for(int i=1;i<=n;i++) { int a;read(a); if(a==1) res^=1;//统计1的个数 } if(res&1)//为奇数则rabbit赢 puts("rabbit"); else//为偶数则hamster赢 puts("hamster"); } return 0; }

谢谢!!!

最新回复(0)