-5

我有一个问题:

当约翰还是个小孩的时候,他没有太多事情可做。没有互联网,没有 Facebook,也没有可以破解的程序。所以他做了他唯一能做的事……他评估了琴弦的美,以寻找世界上最美丽的琴弦。

给定一个字符串 s,小约翰尼将字符串之美定义为其中字母之美的总和。每个字母的美是 1 到 26 之间的整数,包括 1 和 26,没有两个字母具有相同的美。约翰尼不关心字母是大写还是小写,所以这不会影响字母的美观。(例如,大写的“F”与小写的“f”完全一样。)

你是一名学生,正在撰写有关这位著名黑客的青春期的报告。你找到了约翰尼认为最漂亮的绳子。这个字符串的最大可能美是什么?

输入样本:

你的程序应该接受一个文件名的路径作为它的第一个参数。该文件中的每一行都有一个句子。例如

ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves

输出样本:

打印出字符串的最大美感。例如

152
754
491
729
646

这是我在 C# 中的代码:

static void Main(string[] args)
{
    StreamReader myR = new StreamReader(args[0]);
    List<string> myL = new List<string>();


    while (!myR.EndOfStream)
    {
        myL.Add(myR.ReadLine());
    }

    foreach (string item in myL)
    {
        int maxBeautifulNum = 0;
        string temp = item.ToLower();

        char[] myC = temp.ToCharArray();

        string[] tempS = new string[myC.Length];
        string tempCount;

        for (int i = 0; i < myC.Length; i++)
        {
            if ((myC[i] >= 'a' && myC[i] <= 'z') || (myC[i] >= 'A' && myC[i] <= 'Z'))
            {
                tempS[i] = myC[i].ToString();
            }

        }

        tempS = tempS.Where(w => !string.IsNullOrWhiteSpace(w)).ToArray();

        List<string> myCounterList = new List<string>();

        while(tempS.Length >= 1)
        {
            int count = 0;
            tempCount = tempS[0];

            for (int j = 0; j < tempS.Length; j++)
            {
                if (tempS[j] == tempCount)
                {
                    count++;
                }
            }

            myCounterList.Add(count.ToString());

            tempS = tempS.Where(w => w != tempCount).ToArray();
        }

        string[] myCounterString = myCounterList.ToArray();
        Array.Sort(myCounterString);
        Array.Reverse(myCounterString);

        for (int i = 0; i < myCounterString.Length; i++)
        {
            maxBeautifulNum += (26 - i) * int.Parse(myCounterString[i]);
        }
        Console.WriteLine(maxBeautifulNum);
    }
}

它只通过一些测试就可以正常运行,但我找不到其他测试它运行失败。任何人都可以告诉我测试我的代码运行失败。

4

1 回答 1

1

这是一个计算字符串“美”的基本LINQPad程序:

void Main()
{
    string[] inputs =
    {
        "a",
        "z",
        "A test",
        "A TEST",
        "a test"
    };

    foreach (var input in inputs)
    {
        var beauty =
            (from c in input
             let C = char.ToUpper(c)
             where C >= 'A' && C <= 'Z'
             select (int)(C - 'A') + 1).Sum();
        beauty.Dump(input);
    }
}

所有的魔力都在循环内的单个 LINQ 语句中。

此外,要从文件中读取所有行,请使用以下命令:

string[] lines = File.ReadAllLines(fileName);
于 2013-10-15T15:58:47.197 回答