来自重粒子的小程序
发表于:2007-06-30来源:作者:点击数:
标签:
Palin.csbr --------------br using System;br using System.Collections;br br struct Pld{br nbsp;nbsp;nbsp;nbsp;public int level;br nbsp;nbsp;nbsp;nbsp;public String str;br }br br class Palin{br nbsp;nbsp;nbsp;nbsp;public static int Main(strin
Palin.cs<br>
--------------<br>
using System;<br>
using System.Collections;<br>
<br>
struct Pld{<br>
public int level;<br>
public String str;<br>
}<br>
<br>
class Palin{<br>
public static int Main(string[] args){<br>
if (args.Length != 1){<br>
Console.WriteLine("Usage: one argument");<br>
return -1;<br>
}<br>
Queue queuePalin = new Queue();<br>
Pld currState;<br>
int i,j,len;<br>
String palindrome;<br>
<br>
currState.level = 0;<br>
currState.str = args[0];<br>
queuePalin.Enqueue(currState);<br>
<br>
while(queuePalin.Count > 0){<br>
currState = (Pld)queuePalin.Dequeue();<br>
palindrome = (String)currState.str;<br>
len = palindrome.Length;<br>
<br>
for(i = 0, j = len - 1;i < j && palindrome[i] == palindrome[j];<br>
i++,j--);<br>
if( i < j ){<br>
currState.level++;<br>
currState.str = palindrome.Substring(i+1,j-i);<br>
queuePalin.Enqueue(currState);<br>
currState.str = palindrome.Substring(i,j-i);<br>
queuePalin.Enqueue(currState);<br>
}else{<br>
Console.WriteLine(currState.level);<br>
return 0;<br>
}<br>
} <br>
Console.WriteLine("Error!");<br>
return -2;<br>
}<br>
}<br>
--------------<br>
csc palin.cs<br>
palin dAb3d<br>
--------------<br>
2<br>
--------------<br>
程序如果不好懂,请举手!<br>
<br>
嗬嗬,C#的Frameworks居然提供了队列这种数据结构,真不错!!!
原文转自:http://www.ltesting.net