- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
最近我参加了一个编码挑战,但我只得到了 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 5Output:
0
Explanation: Cat
1
can move to either field2
or3
. In the next move cat2
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;
}
}
}
最佳答案
让我们来分析一下比赛。当一只猫搬家输了比赛?首先,每只猫的位置并不重要,重要的是猫之间的距离:如果在 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 == 3
和 3 % 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/
我正在尝试使用我正在调用的函数构建一个 vector vector CompPop() .我想返回类型为 vector 的 vector 信息.下面是我的函数代码,用于返回我的 Competition
在“AmDocs”最近组织的比赛中,我遇到了以下问题:(问题的基本思路) 给定一个大小固定为 12x12 的矩阵。给定六个长度为 6、5、5、4、3、2 的线段。矩阵有空的空间和填充的空间。您必须返回
当一个线程等待一个条件变量时,关联的互斥锁被(原子地)释放(解锁)。当该条件变量(由不同的线程)发出信号时,一个(对于信号)或所有(对于广播)等待线程被唤醒,自动重新获取(锁定)互斥量。 如果一个或多
我将参加 OBI(巴西信息学奥林匹克竞赛,英语),并且我正在尝试过去几年的一些练习。但是我找不到这个练习的解决方案(我翻译了它,所以可能有一些错误): Chocolate Competition Ca
我试图理解 Steven Halim 和 Felix Halim 的书 Competitive Programming 中的“凸包”算法。 “凸包”问题是,给定平面中 n 个点的集合 P,找到一个子集
我试图在远程 Kaggle 内核中使用 Python 模块,但是当我运行 from kaggle.competitions import nflrush 时,出现了这个错误: Could not fi
我正在尝试将 Z3 与 c++ api(版本 Z3 4.1.0.0)一起使用,即我正在尝试解析来自 smt-competition unsat 核心轨道的实例。我(根据示例)编写了以下代码: cont
最近我参加了一个编码挑战,但我只得到了 50% 的分数。很少有测试用例在执行我的代码时失败,我无法找到代码失败的原因。所以我在下面添加了问题和我的代码。感谢您帮助找到测试用例失败的原因。 要求 Com
我是 kaggler 的新兵。 我 fork 一个开放的内核并提交,当我提交我的输出时,按钮 Submit to Competition不工作,以及“您的内核无法使用互联网访问来参加本次比赛”的信息。
我正在研究一些数据结构,我注意到这是一个时间复杂度: O(log(log(n))))-竞争性。 我读到持续竞争是预期时间/最佳时间的比率。但是,拥有一套竞争力意味着什么? 最佳答案 在线算法是一种事先
我正在尝试从 Kaggle 竞赛中下载数据 state-farm-distracted-driver-detection 数据集具有以下目录结构 |-driver_imgs_list.csv |-sa
我正在尝试从 kaggle 比赛中下载数据,但我遇到了标题问题。我已经搜索过,我知道问题是我正在尝试为 kaggle 内核而不是本地内核运行它,但我不知道如何解决这个问题。我知道这是一个愚蠢的问题,但
我刚刚开始使用 OpenCL 1.2 和 C++ 绑定(bind)。我想将写入缓冲区异步排队并在操作完成后获取回调。这是相关代码行的精简版本: cl::Event enqueuingBufferRea
我是一名优秀的程序员,十分优秀!