数字を時計回り(または反時計回り)に 180 度回転させて同じ数字にする問題を考えました。3、4、および 7 を除くすべての数字では、回転された数字が有効な数字です。(3 回転すると ε が得られます)。私はC#が初めてですが、なんとか解決しました。
public static bool MyMethod(int originalInt)
{
bool is180 = false;
if (Array.IndexOf(originalInt.ToString().ToArray(), '3') > -1 || Array.IndexOf(originalInt.ToString().ToArray(), '4') > -1 || Array.IndexOf(originalInt.ToString().ToArray(), '7') > -1)
{
return false;
}
else
{
List<int> tempList = new List<int>();
int tempInt = originalInt;
do
{
int lastDigit = tempInt % 10;
if (lastDigit == 9)
{
lastDigit = 6;
}
else if (lastDigit == 6)
{
lastDigit = 9;
}
tempInt = tempInt / 10;
tempList.Add(lastDigit);
}
while (tempInt > 0);
tempList.Reverse();
int tempInt2 = originalInt;
int lastDigit2 = 0;
foreach (int item in tempList)
{
lastDigit2 = tempInt2 % 10;
if (item == lastDigit2)
{
is180 = true;
tempInt2 = tempInt2 / 10;
}
else
{
return false;
}
}
}
return is180;
}
これをもっと簡単に解決する方法を見つけることができますか? ありがとうございました。