当前位置: 代码迷 >> 综合 >> 智力题
  详细解决方案

智力题

热度:25   发布时间:2024-02-02 12:57:49.0

1.涂色

n*m的格子,选一种颜色涂色,不能相邻。考虑到n*m相乘奇数和偶数的情况。

class Paint {
public:
int getMost(int n, int m) {
return (n * m + 1) >> 1;
}
};

2.赛马

输入:

[1,2,3],[1,2,3],3

输出:2

尽可能多胜出

class HorseRace {
public:
int winMost(vector<int> oppo, vector<int> horses, int n) {

sort(oppo.begin(),oppo.end());
sort(horses.begin(),horses.end());
int i=0,j=0;
while(i<n&&j<n)
{
if(horses[i]>oppo[j])
{
++i;
++j;
}
else
++i;
}
return j;
}
};

3.跳格子

一次只能跳1-3格,自己先跳,会会赢(假设都不会犯错误)

把所有格子分为4格一段...,然后考虑每一段

class Jump {
public:
int checkWin(int n) {

if((n-1)%4==0)
return 0;
else
return 1;
}
};

4.小游戏

从1,m到n,1,A先走,谁会赢

class Game {
public:
char getWinner(int n, int m) {

if((n*m)%2==0)
return 'A';
else
return 'B';
}
};

5.nim游戏

class Clear {
public:
int getWinner(vector<int> A, int n) {

sort(A.begin(),A.end());
int flag=0,j=0,res=0;
for(int i=0;i<n;++i)
{
j=i;
flag=0;
while(j<n&&A[j]==A[i]) ++flag,++j;
res^=flag;
i=j-1;
}
return res!=0;
}
};

  相关解决方案