gpt4 book ai didi

c# - 编程挑战 : Competition for the best cat is coming

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:05:24 25 4
gpt4 key购买 nike

最近我参加了一个编码挑战,但我只得到了 50% 的分数。很少有测试用例在执行我的代码时失败,我无法找到代码失败的原因。所以我在下面添加了问题和我的代码。感谢您帮助找到测试用例失败的原因

要求

Competition for the best cat is coming. The competition consists of several duels. In each duel both cats are standing on an infinite line facing each other. The older cat starts - it has to move towards his opponent by 1 or 2 cat-units. Neither of them can go back, jump over his opponent or share the same spot. The cat owners trained their cats the best they could, so their pets play optimally. The cat that is unable to make a move loses.

输入

First line of the input contains number of tests t (t ≤ 1000). Each test consists of 4 integers: age and position of the first cat and then age and position of the second cat. No two cats have the same age. No calculations in this task will exceed the number 2**31 - 1.

输出

For each test print 1 if the first cat wins and 0 otherwise.

示例

Input:

1
10 1 9 5

Output:

0

Explanation: Cat 1 can move to either field 2 or 3. In the next move cat 2 regardless of the opponent's decision can block him and win.

我的代码

using System;
using System.Linq;

namespace Solution
{
class Solution
{
static void Main(string[] args)
{
var input1 = Console.ReadLine().Trim();
var numOfTest = Convert.ToInt32(input1);

for (int i = 0; i < numOfTest; i++)
{
var input2 = Console.ReadLine().Trim();
var catDetails = input2.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToInt32(x)).ToArray();
int result = -1;

if (catDetails[0] == catDetails[2])
{
Console.WriteLine(-1);
break;
}

if(catDetails[0] > catDetails[2])
result = CalculateWinningResult(catDetails[0], catDetails[1], catDetails[2], catDetails[3], 1);
else
result = CalculateWinningResult(catDetails[0], catDetails[1], catDetails[2], catDetails[3], 2);

Console.WriteLine(result);
}
}

private static int CalculateWinningResult(int cat1Age, int cat1Position, int cat2Age, int cat2Position, int moveCat)
{

switch (moveCat)
{
case 1:

cat1Position = CalculateCatPosition(cat1Position, cat2Position);
if (cat1Position == 0)
return 0;
else
return CalculateWinningResult(cat1Age, cat1Position, cat2Age, cat2Position, 2);
case 2:
cat2Position = CalculateCatPosition(cat2Position, cat1Position);
if (cat2Position == 0)
return 1;
else
return CalculateWinningResult(cat1Age, cat1Position, cat2Age, cat2Position, 1);
}
return 0;
}

private static int CalculateCatPosition(int currentCatPosition, int opponentCatPosition)
{
int tempPosition;
if (currentCatPosition > opponentCatPosition)
{
tempPosition = (currentCatPosition - 1);

if (tempPosition <= opponentCatPosition)
return 0;
else
{
tempPosition = (currentCatPosition - 2);
if (tempPosition >= opponentCatPosition)
return 0;
else
currentCatPosition = tempPosition;
}
}
else
{
tempPosition = (currentCatPosition + 1);

if (tempPosition <= opponentCatPosition)
return 0;
else
{
tempPosition = (currentCatPosition + 2);
if (tempPosition >= opponentCatPosition)
return 0;
else
currentCatPosition = tempPosition;
}
}

return currentCatPosition;
}
}
}

结果 test case result

最佳答案

让我们来分析一下比赛。当一只猫搬家输了比赛?首先,每只猫的位置并不重要,重要的是猫之间的距离:如果在 10 1 9 5 中测试移动中的猫比 10 101 9 105 移动中的猫也输了。

现在,让我们画一个简单的表格:

   distance | cat on move
----------------------
0 | Loses (evident: he is blocked)
1 | Wins
2 | Wins
3 | Loses
4 | Wins
5 | Wins
6 | Loses
7 | Wins
8 | Wins
9 | Loses
10 | Wins
11 | Wins
12 | Loses
....

你能看到模式吗?如果猫之间的距离能被3整除,则移动中的猫输;否则他(猫是“他”或“她”,绝不是“它”!)赢了,他的策略是保持他和他的对手之间的距离被3整除。你可以用归纳法来证明它。

例如:对于 10 1 9 5 情况,距离是 5 - 1 - 1 == 33 % 3 == 0(距离可以被 3 整除)所以移动中的猫(10 岁)松了。

伪代码:(我们不要破坏乐趣;请自己实现 C# 代码)。当给定猫的年龄和初始位置的 4 个数字 age1 x1 age2 x2 时,我们可以按如下方式找出获胜者:

 if ((Abs(x2 - x1) - 1) % 3 == 0) then // if distance divisible by 3?
// Yes: Cat's on move loses
if (age1 > age2) then
return 1
else
return 0
else
// No: Cat's on move wins
if (age1 > age2) then
return 0
else
return 1

关于c# - 编程挑战 : Competition for the best cat is coming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53954854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com