-1

我创建了一种确定两个字符串是否已排序的方法。我所有的测试用例都可以工作,除了一个。代码如下。

class Program
{
    static void Main(string[] args)
    {
        bool test1 = isSorted("test1", "test2");
        bool test2 = isSorted("4576", "4567");
        bool test3 = isSorted("test10", "test11");
        bool test4 = isSorted("abdc", "abcd");
    }

    public static bool isSorted(string MyFirstString, string MySecondString)
    {
        string MyFirstCutString = MyFirstString.ToLower();
        string MySecondCutString = MySecondString.ToLower();

        if (MyFirstString.Length > MySecondString.Length)
            MyFirstCutString = MyFirstCutString.Substring(0, MySecondString.Length);
        else if (MySecondString.Length > MyFirstString.Length)
            MySecondCutString = MySecondCutString.Substring(0, MyFirstCutString.Length);

        for (int i = 0; i < MyFirstCutString.Length; i++)
        {
            if (getNumberic(MyFirstCutString[i]) > getNumberic(MySecondCutString[i]))
                return false;
        }

        return true;
    }

    public static int getNumberic(char myLetter)
    {
        switch (myLetter)
        {
            case 'a':
                return 1;
            case 'b':
                return 2;
            case 'c':
                return 3;
            case 'd':
                return 4;
            case 'e':
                return 5;
            case 'f':
                return 6;
            case 'g':
                return 7;
            case 'h':
                return 8;
            case 'i':
                return 9;
            case 'j':
                return 10;
            case 'k':
                return 11;
            case 'l':
                return 12;
            case 'm':
                return 13;
            case 'n':
                return 14;
            case 'o':
                return 15;
            case 'p':
                return 16;
            case 'q':
                return 17;
            case 'r':
                return 18;
            case 's':
                return 19;
            case 't':
                return 20;
            case 'u':
                return 21;
            case 'v':
                return 22;
            case 'w':
                return 23;
            case 'x':
                return 24;
            case 'y':
                return 25;
            case 'z':
                return 26;
            case 'O':
                return 27;
            case '1':
                return 28;
            case '2':
                return 29;
            case '3':
                return 30;
            case '4':
                return 31;
            case '5':
                return 32;
            case '6':
                return 33;
            case '7':
                return 34;
            case '8':
                return 35;
            case '9':
                return 36;
            default:
                return 1000;
        }
    }
}

测试结果如下:

test1 result is true
test2 result is false
test3 result is false (NOT GOOD!)
test4 result is true

我看不出我的逻辑有任何缺陷。

4

3 回答 3

7

你混在了交换机上O0这意味着0获取数值1000,并在后面排序1

于 2012-04-11T14:59:05.210 回答
1

而不是0你有OgetNumberic 方法。

顺便说一句,我至少会这样重写它:

    public static int getNumberic( char c )
    {
        if( c >= 'a' && c <= 'z' )
        {
            return c - 'a';
        }
        else if( c >= '0' && c <= '9' )
        {
            return 'z' + c - '0';
        }
        else
        {
            return 1000;
        }
    }
于 2012-04-11T14:59:28.463 回答
0

不是您搜索的答案,而是您对此有何看法?

public bool isSorted(string s1, string s2)
{
    return s1.ToLower().CompareTo(s2.ToLower()) < 0;
}

或者

public bool isSorted(string s1, string s2)
{
    return String.Compare(s1, s2, true) < 0;
}
于 2012-04-11T15:04:38.577 回答