【发布时间】:2015-11-07 19:35:23
【问题描述】:
我在 CodeinGame 中解决了一个问题,我必须将 Thor 带到灯光处在网站上例如: 如果初始 pos 是 (5,4) 它通过 Log 但如果初始 pos 是 (31,17) 它会失败 Log
我的代码
string[] inputs = Console.ReadLine().Split(' ');
int lightX = int.Parse(inputs[0]); // the X position of the light of power
int lightY = int.Parse(inputs[1]); // the Y position of the light of power
int initialTX = int.Parse(inputs[2]); // Thor's starting X position
int initialTY = int.Parse(inputs[3]); // Thor's starting Y position
// game loop
int thorx=initialTX;
int thory=initialTY;
string directionX, directionY;
while (true)
{
int remainingTurns = int.Parse(Console.ReadLine()); // The remaining amount of turns Thor can move. Do not remove this line.
// Write an action using Console.WriteLine()
// To debug: Console.Error.WriteLine("Debug messages...");
if (thorx>lightX)
{
directionX="W";
thorx=-1;
Console.WriteLine("W");
}
else if (thorx<lightX)
{
directionX="E";
thorx=+1;
Console.WriteLine("E");
}
else
{
if (thory>lightY)
{
directionY="N";
thory=-1;
Console.WriteLine("N");
}
else if (thory<lightY)
{
directionY="S";
thorx=+1;
Console.WriteLine("S");
}
}
CodeinGame Link这是第二题雷神之力
【问题讨论】:
-
好的,你有问题吗?
-
@Steve 编辑了问题
-
检查你的雷神南下代码。
标签: c# if-statement logic