【问题标题】:WAP to find whether a string is palindrome or not in C#WAP 在 C# 中查找字符串是否为回文
【发布时间】:2017-03-04 08:22:43
【问题描述】:
using System;
  using System.Collections.Generic;
    using System.Linq;
  using System.Text;
 using System.Threading.Tasks;

    namespace PelindromeIdentifier
  {
  class Program
  {
    static void Main()
    {
        int nNumberOfTestCases = 0;
        int.TryParse(Console.ReadLine(), out nNumberOfTestCases);

        String[] strTestCases = new String[nNumberOfTestCases];
        char[] arrChar;
        int nCharCount = 0;
        bool bAllow = false;
        int nCharIndex = 0;

        for (int nTestCase = 0; nTestCase < nNumberOfTestCases; nTestCase++)
        {
            strTestCases[nTestCase] = Console.ReadLine();
        }

        for (int nTestCase = 0; nTestCase < nNumberOfTestCases; nTestCase++)
        {
            bAllow = false;

            arrChar = strTestCases[nTestCase].Distinct().ToArray();

            for (nCharIndex = 0; nCharIndex < arrChar.Length; nCharIndex++)
            {
          nCharCount = strTestCases[nTestCase].Count(i =>                                     i.Equals(arrChar[nCharIndex]));

                if (strTestCases[nTestCase].Length % 2 == 0)
                {
                    if (nCharCount % 2 != 0)
                    {
                        Console.WriteLine("NO");
                        break;
                    }
                }
                else
                {
                    if (nCharCount % 2 != 0)
                    {
                        if (bAllow == true)
                        {
                            Console.WriteLine("NO");
                            break;
                        }
                        bAllow = true;
                    }
                }
            }

            if (nCharIndex == arrChar.Length)
            {`
                Console.WriteLine("YES");
            }
            }
            }
           }
              }

我试过了,但是有一些隐藏的测试用例不起作用。有人能帮帮我吗?在这个程序中,程序会询问用户你想要输入多少次字符串,并且对于每个字符串,它会返回字符串是否是不是回文

【问题讨论】:

  • 只是帮助可能的测试用例是什么
  • 隐藏测试用例?认真的吗?
  • 我不明白你想用你的代码做什么。您是否真的试图让您的代码尽可能难以理解?

标签: c#


【解决方案1】:

不确定您所说的可能的测试用例是什么意思: 我已经更改了您的代码并使用 3 个字符串进行了测试:

"coucou" : 不是回文

“皮划艇”:回文

“abcdeffedcba”:回文

现在可以正常使用了:

 static void Main()
    {
        int nNumberOfTestCases = 0;
        var input = 
        int.TryParse(Console.ReadLine(), out nNumberOfTestCases);

        String[] strTestCases = new String[nNumberOfTestCases];

        for (int nTestCase = 0; nTestCase < nNumberOfTestCases; nTestCase++)
        {
            strTestCases[nTestCase] = Console.ReadLine();
        }

        for (int nTestCase = 0; nTestCase < nNumberOfTestCases; nTestCase++)
        {

            if (strTestCases[nTestCase].SequenceEqual(strTestCases[nTestCase].Reverse()))
                Console.WriteLine("YES");
            else
                Console.WriteLine("NO");


        }
        Console.ReadKey();
    }

【讨论】:

  • 如果我们将空字符串作为输入会发生什么?
  • 如果我们将空字符串作为输入,它将返回 YES,但如果您希望在这种特殊情况下返回 no,您可以修改这一行: if (!string.IsNullOrEmpty(strTestCases[nTestCase] ) && strTestCases[nTestCase].SequenceEqual(strTestCases[nTestCase].Reverse()))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 2012-04-05
相关资源
最近更新 更多