2

我认为将数字顺时针(或 ccw)旋转 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;
        }

你能找到一种更简单的解决方法吗?谢谢你。

4

3 回答 3

3

伪代码:

map['0'] = '0';
map['1'] = '1';
map['2'] = '2';
map['5'] = '5';
map['6'] = '9';
map['8'] = '8';
map['9'] = '6';

for each position i in input_string :
    if map index exists for input_string[i] :
        rotated_string[i] = map[input_string[i]]
    else
        exit for

rotated_string = reverse(rotated_string)

if input_string = rotated_string :
    has_rotational_symmetry = true
else
    has_rotational_symmetry = false
于 2013-10-16T22:53:41.777 回答
1

我不是 100% 确定你在问什么.. 但以下为你的方法正确返回 true 和 false ......

编辑:现在有了 Lippertization!

public static bool MyMethod(int originalInt)
{
        var s = originalInt.ToString();
        return !(s.Contains('3') || s.Contains('4') || s.Contains('7'));
}
于 2013-10-16T22:55:13.737 回答
0

无法抗拒 F# 版本:

let is180 s =
    let rd = function|'0'->'0'|'1'->'1'|'2'->'2'|'5'->'5'|'6'->'9'|'8'->'8'|'9'->'6'|_->'X'
    let flip x = new string(x |> Seq.map rd |> Seq.toArray |> Array.rev)
    s.Equals(flip s)
于 2013-10-17T00:28:51.487 回答