3. 這不是河內塔喔!
嘗試從C丟回A、B,整體視作一個Stack結構
# 記住可以 B 連續丟到 A,所以 else 很重要,這樣才不會提早 break
string s;
while ((s = Console.ReadLine()) != null)
{
if (s == "0")
{
Console.WriteLine();
continue;
}
if (!s.Contains(" ")) continue;
var data = s.Split(' ');
int n = data.Length;
var C = new Stack<int>();
var B = new Stack<int>();
for (global::System.Int32 i = (data.Length) - (1); i >= 0; i--)
C.Push(int.Parse(data[i]));
bool y = false;
while (n > 0)
{
if (B.TryPeek(out var x) && x == n)
{
n--;
B.Pop();
}
else {
if (!C.TryPop(out x))
{
Console.WriteLine("NO!");
y = true;
break;
}
if (x == n)
n--;
else B.Push(x);
}
}
if (!y)
Console.WriteLine("YES!");
}Last updated