我正在尝试编写一个程序,它将从文本文件中的单词写出最长和最短的回文。我的代码现在看起来像这样:
static void Main(string[] args)
{
string[] lines = System.IO.File.ReadAllLines(@"C:\palindromy.txt");
foreach (string line in lines)
{
char[] charArray = line.ToCharArray();
for (int i = 0; i < 1; i++)
{
Array.Reverse(charArray);
bool a = charArray.SequenceEqual(line);
while(a == true)
{
Console.WriteLine(line); /that will just write all palindroms
break;
}
}
}
}
我目前正在写所有的回文,但我需要写出最长和最短的回文。
答案 0 :(得分:0)
static void Main(string[] args)
{
string[] lines = System.IO.File.ReadAllLines(@"C:\palindromy.txt");
private string longest;
private string shortest;
foreach (string line in lines)
{
char[] charArray = line.ToCharArray();
for (int i = 0; i < 1; i++)
{
Array.Reverse(charArray);
bool a = charArray.SequenceEqual(line);
if(a)
{
if(a.Length > longest.length)
{
longest = a;
}
if(a.Length<shortest.length)
{
shortest = a;
}
}
}
}
Console.WriteLine(longest);
Console.WriteLine(shortest);
}
从头顶看,它看起来像这样。
答案 1 :(得分:0)
与之前的答案类似,但提出以下建议
检查您是否找到最短时间时,需要记住这一点 shortest.Length与0开始,所以你会想要存储最短的第一个回文,如果最短的话.Length == 0.
static void Main(string[] args)
{
var lines = System.IO.File.ReadAllLines(@"C:\palindromy.txt");
string longest = string.Empty;
string shortest = string.Empty;
foreach (var line in lines)
{
if (line.Length == 0) continue;
char[] charArray = line.ToCharArray();
Array.Reverse(charArray);
string reversedLine = new string(charArray);
if (line.Equals(reversedLine, StringComparison.OrdinalIgnoreCase))
{
if (line.Length > longest.Length)
{
longest = line;
}
if (line.Length < shortest.Length || shortest.Length == 0)
{
shortest = line;
}
}
}
Console.WriteLine(longest);
Console.WriteLine(shortest);
}
答案 2 :(得分:0)
如果你使用一些LINQ,你可以在很大程度上简化它:
string[] lines = System.IO.File.ReadAllLines(@"C:\palindromy.txt");
var palindromes = lines.Where(x => x == new string(x.ToCharArray()
.Reverse()
.ToArray()));
var shortest = palindromes.Aggregate((a, b) => a.Length < b.Length ? a : b);
var longest = palindromes.Aggregate((a, b) => a.Length > b.Length ? a : b);
Console.WriteLine("The shortest palindrome is {0}.", shortest);
Console.WriteLine("The longest palindrome is {0}.", longest);
答案 3 :(得分:0)
这是一个简单的控制台应用程序,可以完全满足您的要求。单词位于List
,因此您必须阅读文本文件中的单词并将其存储在List
中。
using System;
using System.Collections;
using System.Collections.Generic;
namespace PalinDromes
{
class MainClass
{
public static void Main (string[] args)
{
List<string> words = new List<string> () { "anotherword","civic", "deified", "lemel", "pop" ,"noon"};
string shortest = "";
string longest = "";
foreach (string word in words)
{
if (isPalindrome (word))
{
if (word.Length > longest.Length)
longest = word;
else if (shortest == "" || word.Length < shortest.Length)
shortest = word;
}
}
Console.WriteLine ("Shortest: {0}", shortest);
Console.WriteLine ("Longest: {0}", longest);
}
public static bool isPalindrome(string word)
{
return word == ReverseString (word);
}
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
}