位置:海鸟网 > IT > ASP.NET >

来自重粒子的C#程序

Palin.cs
--------------
using System;
using System.Collections;

struct Pld{
public int level;
public String str;
}

class Palin{
public static int Main(string[] args){
if (args.Length != 1){
Console.WriteLine("Usage: one argument");
return -1;
}
Queue queuePalin = new Queue();
Pld currState;
int i,j,len;
String palindrome;

currState.level = 0;
currState.str = args[0];
queuePalin.Enqueue(currState);

while(queuePalin.Count > 0){
currState = (Pld)queuePalin.Dequeue();
palindrome = (String)currState.str;
len = palindrome.Length;

for(i = 0, j = len - 1;i < j && palindrome[i] == palindrome[j];
i++,j--);
if( i < j ){
currState.level++;
currState.str = palindrome.Substring(i+1,j-i);
queuePalin.Enqueue(currState);
currState.str = palindrome.Substring(i,j-i);
queuePalin.Enqueue(currState);
}else{
Console.WriteLine(currState.level);
return 0;
}
}
Console.WriteLine("Error!");
return -2;
}
}
--------------
csc palin.cs
palin dAb3d
--------------
2
--------------
程序如果不好懂,请举手!

嗬嗬,C#的Frameworks居然提供了队列这种数据结构,真不错!!!