C#就是好玩,用pascal用惯了用这个简直舒服死,vs.net和turbo pascal肯定不是一个级别的.pascal不面向对象,弄得我们想做棵树都得定record然后放在数组里面,c#可以直接指向,而且本身就是一个对象..不需要用什么怪异的数组存...速度么,没仔细测,不过都是瞬解.呵呵.偶的第一个程序是helloworld,这个么,是第一个像样的程序...
嗯,不废话咯
FBI树 (fbi.pas/dpr/c/cpp)
【问题描述】
我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串。 FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三种。由一个长度为2N的“01”串S可以构造出一棵FBI树T,递归的构造方法如下: 1) T的根结点为R,其类型与串S的类型相同; 2) 若串S的长度大于1,将串S从中间分开,分为等长的左右子串S1和S2;由左子串S1构造R的左子树T1,由右子串S2构造R的右子树T2。 现在给定一个长度为2N的“01”串,请用上述构造方法构造出一棵FBI树,并输出它的后序遍历序列。
【输入文件】
输入文件fbi.in的第一行是一个整数N(0 <= N <= 10),第二行是一个长度为2N的“01”串。
【输出文件】
输出文件fbi.out包括一行,这一行只包含一个字符串,即FBI树的后序遍历序列。
【样例输入】
3 10001011
【样例输出】
IBFBBBFIBFIIIFF
【数据规模】
对于40%的数据,N <= 2; 对于全部的数据,N <= 10。
|
using System; namespace FBI { /// <summary> /// Class1 的摘要说明。 /// </summary> public class tree { public char data=new char(); public tree lc=null,rc=null; public tree(string s) { this.data=mydata(s); if(s.Length!=1) { this.lc=mychild(s.Substring(0,s.Length/2)); this.rc=mychild(s.Substring(s.Length/2,s.Length/2)); } } private char mydata(string s) { char ch=s[0]; for (int i=0;i<s.Length;i++) { if(s[i]!=ch){return 'F';} } if(ch=='0') {return 'B';} else {return 'I';} } private tree mychild(string s) { tree mytree=new tree(s); mytree.mydata(s); return mytree; } public void show() { if(this.lc != null){this.lc.show();} if(this.rc != null){this.rc.show();} Console.Write(this.data); } } class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static int sqr(int a) { int c=new int(); c=1; for (int i=0;i<a;i++) { c=2*c; } return c; } static void Main(string[] args) { string s; int n=0; //n = System.Convert.ToInt32(System.Console.ReadLine()); //s = System.Convert.ToString(System.Console.ReadLine()); n=3; s="10001011"; Console.WriteLine(sqr(n));//感觉N没用,当然是不给非法参数的时候 tree mytree=new tree(s); mytree.show(); Console.ReadLine(); // // TODO: 在此处添加代码以启动应用程序 // } } }
|