数据结构和算法 - C#语言描述

 

//平时下班自己学习用的

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace DSC1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Name myName = new Name("wang", "wei", "guo");
            //string fullName, inits;
            //fullName = myName.ToString();
            //inits = myName.Initials();
            //Console.WriteLine("My Name is {0}", fullName);
            //Console.WriteLine("Init is {0}", inits);
            //Console.ReadLine();

            //Collection names = new Collection();
            //names.Add("David");
            //names.Add("bernica");

            //foreach(Object name in names)
            //{
            //    Console.WriteLine(name);
            //}

            //Console.WriteLine("Count : " + names.Count());

            //Console.ReadLine();

            //int num1 = 100;
            //int num2 = 200;

            //Swap<int>(ref num1, ref num2);
            //Console.WriteLine(num1);
            //Console.WriteLine(num2);
            //Console.ReadLine();

            //int[] nums = new int[10000];
            //BuildArray(nums);
            //TimeSpan duration;
            //Timing2 time2 = new Timing2();
            //time2.startTime();
            //DisplayNums(nums);
            //DisplayNums(nums);
            //DisplayNums(nums);
            //time2.stopTime();
            //Console.WriteLine(time2.Result().TotalSeconds);
            ////duration = Process.GetCurrentProcess().TotalProcessorTime;
            ////Console.WriteLine(duration.TotalSeconds);
            //Console.ReadLine();

            //int[] numbers;
            //numbers = new int[] { 1, 2, 3 };
            //Type arrayType = numbers.GetType();
            //if(arrayType.IsArray)
            //{
            //    Console.WriteLine("This is a Array");
            //}
            //else
            //{
            //    Console.WriteLine("This is not a Array");

            //}
            //Console.ReadLine();

            //int[,] grades = new int[,]
            //{
            //    {1,82,74,89,100},
            //    {2,93,96,85,86}
            //};

            //int last_grade = grades.GetUpperBound(1);
            //double average = 0.0;
            //int total;
            //int last_student = grades.GetUpperBound(0);  //最后一个学生的index
            //for(int row=0;row<=last_student;row++)
            //{
            //    total = 0;
            //    for(int col=1;col<=last_grade;col++)
            //    {
            //        total += grades[row, col];
            //    }
            //    average = total / last_grade;
            //    Console.WriteLine(average);
            //}

            //ArrayList names = new ArrayList();
            //names.Add("Mike");
            //names.Add("Beata");
            //Console.WriteLine("The Original list of names: ");
            //foreach(Object name in names)
            //{
            //    Console.Write(name + " ");
            //}

            //string[] newNames = new string[] { "David", "Michael" };
            //ArrayList moreNames = new ArrayList();
            //moreNames.Add("wwg");

            //names.InsertRange(0, newNames);
            //names.AddRange(moreNames);

            //foreach (Object name in names)
            //{
            //    Console.Write(name + " ");
            //}


            //CArray nums = new CArray(10);
            //Random rnd = new Random(100);

            //for (int i = 0; i < 10;i++ )
            //{
            //    nums.Insert(rnd.Next(0,100));
            //}

            //nums.DisplayElements();

            //nums.SelectionSort();

            //Timing2 sortTime = new Timing2();
            //Random rnd = new Random(10000);
            //int numItems = 10000;
            //CArray theArray = new CArray(numItems);
            //for (int i = 0; i < numItems;i++ )
            //{
            //    theArray.Insert(rnd.Next(0,10000));
            //}

            //sortTime.startTime();
            //theArray.SelectionSort();
            //sortTime.stopTime();
            //Console.WriteLine("SelectionSort : " + sortTime.Result().TotalMilliseconds);
            //theArray.Clear();

            //for (int i = 0; i < numItems; i++)
            //{
            //    theArray.Insert((int)(rnd.NextDouble() * 100));
            //}

            //sortTime.startTime();
            //theArray.BubbleSort();
            //sortTime.stopTime();
            //Console.WriteLine("BubbleSort : " + sortTime.Result().TotalMilliseconds);
            //theArray.Clear();

            //int[] numbers = new int[100];
            //StreamReader numFile = File.OpenText(@"d:/wwgData/numbers.txt");
            //for (int i = 0; i < numbers.Length; i++)
            //{
            //    numbers[i] = Convert.ToInt32(numFile.ReadLine(), 10);
            //}

            //int searchNumber;
            //Console.Write("Enter a number to search for: ");
            //searchNumber = Convert.ToInt32(Console.ReadLine(), 10);

            //bool found;
            //found = SeqSearch(numbers, searchNumber);

            //Random random = new Random();
            //CArray mynums = new CArray(10);
            //for (int i = 0; i <= 9; i++)
            //{
            //    mynums.Insert(random.Next(10));
            //}
            //mynums.BubbleSort();

            //mynums.DisplayElements();
            //int position = mynums.binSearch(7);
            //if (position>-1)
            //{
            //    Console.WriteLine("found it");
            //}
            //else
            //{
            //    Console.WriteLine("not found it");
            //}

            ////回文
            //CStack alist = new CStack();
            //string ch;
            //string word = "sees";
            //bool ishui = true;
            //for (int x = 0; x < word.Length;x++ )
            //{
            //    alist.push(word.Substring(x, 1));
            //}
            //int pos = 0;
            //while(alist.count>0)
            //{
            //    ch = alist.pop().ToString(); //出栈,是最后一个字母
            //    if(ch != word.Substring(pos,1))
            //    {//不是回文
            //        ishui = false;
            //        break;
            //    }

            //    pos++;
            //}

            //if (ishui)
            //{
            //    Console.WriteLine("is hui");
            //}
            //else
            //{
            //    Console.WriteLine("no hui");

            //}


            //使用Stack完成表达式计算
            //Stack nums = new Stack();
            //Stack ops = new Stack();

            //string expression = "1 + 2 + 3 + 5";
            //Calculate(nums, ops, expression);
            //Console.WriteLine(nums.Pop());

            //int num, baseNum;
            //num = Convert.ToInt32(Console.ReadLine());
            //baseNum = Convert.ToInt32(Console.ReadLine());
            //MulBase(num, baseNum);

            //Queue[] numQueue = new Queue[10];//用于收集用的
            //int[] nums = new int[] { 91, 46, 85, 15, 92, 35, 31, 22 };
            //for (int i = 0; i < 10;i++ )
            //{
            //    numQueue[i] = new Queue();
            //}

            ////先各位发散
            //RSort(numQueue, nums, DigitType.ones);
            ////收集
            //BuildArray(numQueue, nums);

            ////十位发散
            //RSort(numQueue, nums, DigitType.tens);

            ////收集
            //BuildArray(numQueue, nums);
            //DisplayNums(nums);

            //byte[] ByteSet = new Byte[] { 2};
            //BitArray BitSet = new BitArray(ByteSet);
            //for (int bits = 0; bits <= BitSet.Count - 1; bits++)
            //{
            //    Console.Write(BitSet.Get(bits) + "");
            //}

            //int bits;
            //string[] binNumber = new string[8];
            //byte[] ByteSet = new Byte[] { 5 };
            //BitArray BitSet = new BitArray(ByteSet);
            //bits = 0;

            //int binary = 7;
            //for (int i = 0; i < BitSet.Count - 1;i++ )
            //{
            //    if(BitSet.Get(i)== true)
            //    {
            //        binNumber[binary] = "1";
            //    }
            //    else
            //    {
            //        binNumber[binary] = "0";
            //    }
            //    bits++;
            //    binary--;

            //    if((bits% 8)==0)
            //    {
            //        binary = 7;
            //        bits = 0;
            //        for(int j=0;j<=7;j++)
            //        {
            //            Console.Write(binNumber[j]);
            //        }
            //    }
            //}

            //string string1 = "Hello world";
            //int len = string1.Length;
            //int pos = string1.IndexOf(" ");
            //string firstWord, secondWord;
            //firstWord = string1.Substring(0, pos);
            //secondWord = string1.Substring(pos + 1);
            //Console.Write(firstWord);
            //Console.Write(",");
            //Console.Write(secondWord);

            //string astring = "now is a happy day";
            //ArrayList words = new ArrayList();
            //words = SplitWords(astring);
            //foreach (string word in words)
            //{
            //    Console.Write(word + ",");
            //}

            //string data = "Mike,McMillan,wwg,smm";
            //string[] sdata;
            //char[] delimiter = new char[] { ',' };
            //sdata = data.Split(delimiter, data.Length);
            //foreach(string word in sdata)
            //{
            //    Console.Write(word + " ");
            //}

            //string[] nouns = new string[] { "cat", "dog", "bird", "eggs", "bones" };
            //ArrayList pluralNouns = new ArrayList();
            //foreach(string noun in nouns)
            //{
            //    if(noun.EndsWith("s"))
            //    {

            //    }
            //}

            //string[] words = new string[] { "recieve", "decieve", "reciept" };
            //for (int i = 0; i <= words.GetUpperBound(0);i++ )
            //{
            //    words[i] = words[i].Replace("cie", "cei");
            //    Console.WriteLine(words[i]);
            //}

            //string[,] names = new string[,]
            //{
            //    {"1504","wwg","Ella","steve","Bob"},
            //    {"1133","Elizabeth","Alex","David","Joe"}
            //};

            //for (int outer=0;outer<=names.GetUpperBound(0);outer++)
            //{
            //    for (int inner=0;inner<=names.GetUpperBound(1);inner++)
            //    {
            //        Console.Write(names[outer,inner].PadRight(10)+" ");
            //    }
            //    Console.WriteLine();
            //}

            //string[] names = new string[] { " David", " Raymond", "Mike " };
            //showName(names);

            //StringBuilder stBuff = new StringBuilder("Ken Thompson");
            //Console.WriteLine(stBuff.Length.ToString());
            //Console.WriteLine(stBuff.Capacity.ToString());
            //Console.WriteLine(stBuff.MaxCapacity.ToString());

            //StringBuilder stBuff = new StringBuilder();
            //Console.WriteLine();
            //stBuff.AppendFormat("YOU order is for {0} widges ", 234);
            //Console.WriteLine(stBuff.ToString());

            //StringBuilder stBuff = new StringBuilder("noise in+++++string");
            //stBuff.Remove(9, 5);
            //Console.WriteLine(stBuff);

            //int size = 100000;
            //Timing2 timeSB = new Timing2();
            //Timing2 timeST = new Timing2();
            //for (int i = 0; i <= 0;i++ )
            //{
            //    timeSB.startTime();
            //    BuildSB(size);
            //    timeSB.stopTime();

            //    timeST.startTime();
            //    BuildString(size);
            //    timeST.stopTime();

            //    Console.WriteLine("sb size;" + size + ",time:" + timeSB.Result().TotalMilliseconds);
            //    Console.WriteLine("st size;" + size + ",time:" + timeST.Result().TotalMilliseconds);
            //}


            //string words = "08/14/57 46 02/25/59 45 06/05/85 18 08/14/57 46";
            //string regExp1 = "(?<dates>(//d{2}///d{2}///d{2}))//s(?<ages>(//d{2}))//s";

            //MatchCollection matchSet = Regex.Matches(words, regExp1);
            //int matchPos;
            //if (matchSet.Count > 0)
            //{
            //    foreach (Match aMatch in matchSet)
            //    {
            //        foreach (Capture aCapture in aMatch.Groups["dates"].Captures)
            //        {
            //            Console.WriteLine("date:" + aCapture.ToString());
            //        }
            //        foreach (Capture aCapture2 in aMatch.Groups["ages"].Captures)
            //        {
            //            Console.WriteLine("age:" + aCapture2.ToString());
            //        }

            //    }
            //}

            //string[] words = new string[] { "heal", "heel", "noah", "techno"};
            //string regExp = "h$"; //. 与字符串中任意字符匹配
            //MatchCollection aMatch;

            //foreach (string word in words)
            //{
            //    if (Regex.IsMatch(word, regExp))
            //    {
            //        aMatch = Regex.Matches(word, regExp);
            //        for (int i = 0; i < aMatch.Count; i++)
            //        {
            //            Console.Write(aMatch[i].Value + " ");
            //        }
            //    }

            //}

            //IPAddress myIPs = new IPAddress();
            //myIPs.Add("Mike", "192.111.111.0");
            //myIPs.Add("wwg", "120.0.0.1");
            //Console.WriteLine(myIPs.Count);
            //Console.WriteLine(myIPs.Item("wwg"));
            //myIPs.Clear();
            //Console.WriteLine(myIPs.Count);

            //IPAddress myips = new IPAddress(@"D://data.txt");
            //Console.WriteLine(myips.Count);
            //Console.WriteLine(myips.Item("wwg"));

            //KeyValuePair<string, int>[] gradeBook = new KeyValuePair<string, int>[10];
            //gradeBook[0] = new KeyValuePair<string, int>("McMillan", 99);
            //gradeBook[1] = new KeyValuePair<string, int>("Ruff", 64);

            //for (int i = 0; i <= gradeBook.GetUpperBound(0);i++ )
            //{
            //    Console.WriteLine(gradeBook[i].Key + ":" + gradeBook[i].Value);
            //}

            //string[] names = new string[99];
            //string name;
            //string[] someNames = new string[] { "David", "Jennifer", "Donnie", "Mayno","Raymod","Bernica","Clayton","Beata","Michael","wwg" };
            //int hashVal;
            //for (int i = 0; i < 10;i++ )

            //    name = someNames[i];
            //    hashVal = BetterHash(name, names);
            //    names[hashVal] = name;

            //}

            //ShowDistrib(names);

            //Hashtable symbols = new Hashtable(25);
            //symbols.Add("salary", 100000);
            //symbols.Add("name", "David Durr");
            //symbols.Add("age", 45);
            //symbols.Add("dept", "IT");
            //symbols["sex"] = "Male";
            //Console.WriteLine("The keys are: ");
            //foreach (Object key in symbols.Keys)
            //{
            //    Console.Write(key + ",");
            //}
            //Console.WriteLine();
            //foreach (object key in symbols.Keys)
            //{
            //    Console.WriteLine(key.ToString()+":"+symbols[key].ToString());
            //}

            //LinkList MyList = new LinkList();
            //ListIter iter = new ListIter(MyList);
            //string choice, value;

            //try
            //{
            //    iter.insertAfter("David");
            //    iter.insertAfter("Mike");
            //    iter.InsertBefore("Donnie");

            //    while(true)
            //    {
            //        Console.WriteLine("Enter your choice");
            //        choice = Console.ReadLine().ToLower();

            //        char[] onechar = choice.ToCharArray();

            //        switch (onechar[0])
            //        {
            //            case 'n':
            //                if((!MyList.IsEmpty())&&(!iter.AtEnd()))
            //                {
            //                    iter.NextLink();
            //                }
            //                else
            //                {
            //                    Console.WriteLine("can't move to next link");
            //                }
            //                break;
            //            case 'g':
            //                if(!MyList.IsEmpty())
            //                {
            //                    Console.WriteLine(iter.GetCurrent().Element);
            //                }
            //                else
            //                {
            //                    Console.WriteLine("list is empty");
            //                }
            //                break;
            //            case 'r':
            //                iter.Reset();
            //                break;
            //            case 's':
            //                if(!MyList.IsEmpty())
            //                {
            //                    MyList.ShowList();
            //                }
            //                else
            //                {
            //                    Console.WriteLine("list is empty");
            //                }
            //                break;
            //            case 'a':
            //                Console.WriteLine();
            //                Console.WriteLine("enter a value to insert");
            //                value = Console.ReadLine();
            //                iter.insertAfter(value);
            //                break;
            //            case 'b':
            //                Console.WriteLine();
            //                Console.WriteLine("enter a value to insert");
            //                value = Console.ReadLine();
            //                iter.InsertBefore(value);

            //                break;
            //            case 'c':
            //                break;
            //            case 'x':
            //                break;
            //        }
            //    }

            //}
            //catch (InsertBeforeHeaderException e)
            //{
            //    Console.WriteLine(e.Message);
            //}

            //LinkedListNode<string> node = new LinkedListNode<string>("Mike");
            //LinkedList<string> names = new LinkedList<string>();
            //names.AddFirst(node);
            //LinkedListNode<string> node1 = new LinkedListNode<string>("David");
            //names.AddAfter(node, node1);
            //LinkedListNode<string> node2 = new LinkedListNode<string>("Raymond");
            //names.AddAfter(node1, node2);
            //LinkedListNode<string> node3 = new LinkedListNode<string>(null);
            //LinkedListNode<string> aNode = names.First;

            //while (aNode!=null)
            //{
            //    Console.WriteLine(aNode.Value);
            //    aNode = aNode.Next;
            //}

            //BinarySearchTree nums = new BinarySearchTree();
            //nums.insert(23);
            //nums.insert(45);
            //nums.insert(16);
            //nums.insert(37);
            //nums.insert(3);
            //nums.insert(99);
            //nums.insert(22);

            //nums.InOrder(nums.root);

            //CSet setA = new CSet();
            //CSet setB = new CSet();

            //setA.Add("milk");
            //setA.Add("eggs");
            //setA.Add("bacon");

            //setB.Add("bacon");
            //setB.Add("eggs");
            //setB.Add("bread");

            //Console.WriteLine(setA);
            //Console.WriteLine(setB);

            //CSet setC = new CSet();
            //setC = setA.Union(setB);
            //Console.WriteLine(setC);

            //setC = setA.Difference(setB);
            //Console.WriteLine(setC);

            //CSet setA = new CSet();
            //CSet setB = new CSet();

            //setA.Add(1);
            //setA.Add(2);
            //setA.Add(3);

            //setB.Add(2);
            //setB.Add(3);

            //Console.WriteLine(setA);
            //Console.WriteLine(setB);

            //CSet setC = setA.Union(setB);
            //Console.WriteLine(setC);

            //const int SIZE = 19;
            //ArrayList theArray = new ArrayList(SIZE);
            //Random random = new Random();
            //for (int index = 0; index < SIZE;index++ )
            //{
            //    theArray.Add(random.Next(100) + 1);
            //}

            //Console.WriteLine();
            //foreach(object o in theArray)
            //{
            //    Console.Write(o.ToString()+",");
            //}

            //int[] iArray = (int[])theArray.ToArray(typeof(int));

            //Console.WriteLine();

            //ShellSort(iArray);

            //const int SIZE = 9;
            //Heap aHeap = new Heap(SIZE);
            //Random RandomClass = new Random();
            //for (int i = 0; i < SIZE;i++ )
            //{
            //    int rn = RandomClass.Next(1, 100);
            //    aHeap.Insert(rn);
            //}

            //aHeap.ShowArray();

            //Graph theGrahph = new Graph(13);
            //theGrahph.AddVertex("A");
            //theGrahph.AddVertex("B");
            //theGrahph.AddVertex("C");
            //theGrahph.AddVertex("D");
            //theGrahph.AddVertex("E");
            //theGrahph.AddVertex("F");
            //theGrahph.AddVertex("G");
            //theGrahph.AddVertex("H");
            //theGrahph.AddVertex("I");
            //theGrahph.AddVertex("J");
            //theGrahph.AddVertex("K");
            //theGrahph.AddVertex("L");
            //theGrahph.AddVertex("M");

            //theGrahph.AddEdge(0, 1);
            //theGrahph.AddEdge(1, 2);
            //theGrahph.AddEdge(2, 3);

            //theGrahph.AddEdge(0, 4);
            //theGrahph.AddEdge(4, 5);
            //theGrahph.AddEdge(5, 6);

            //theGrahph.AddEdge(0, 7);
            //theGrahph.AddEdge(7, 8);
            //theGrahph.AddEdge(8, 9);

            //theGrahph.AddEdge(0, 10);
            //theGrahph.AddEdge(10, 11);
            //theGrahph.AddEdge(11, 12);

            //theGrahph.DepthFirstSearch();
            //theGrahph.BreadthFirstSearch();
            //theGrahph.TopSort();
            //theGrahph.Mst();

            //Timing2 tObj = new Timing2();
            //Timing2 tObj2 = new Timing2();
            //int num = 42;
            //long fibNumber;
            //tObj.startTime();
            //fibNumber = recurFib(num);
            //tObj.stopTime();
            //Console.WriteLine("recurFib: " + fibNumber + ",cost: " + tObj.Result().TotalMilliseconds);

            //tObj2.startTime();
            //fibNumber = iterFib(num+1);
            //tObj2.stopTime();
            //Console.WriteLine("iterFib: " + fibNumber + ",cost: " + tObj2.Result().TotalMilliseconds);

            //Console.WriteLine("recurFib: "+recurFib(10));
            //Console.WriteLine("iterFib: " + iterFib(11));

           
            //背包问题
            //int capcity = 16;
            //int[] size = new int[] { 3, 4, 7, 8, 9 };
            //int[] values = new int[] { 4, 5, 10, 11, 13 };
            //int[] totval = new int[capcity + 1];
            //int[] best = new int[capcity + 1];
            //int n = values.Length;
            //for (int j = 0; j <= n - 1;j++)
            //{//遍历所有的物品
            //    for (int i = 0; i <= capcity;i++ )
            //    {//遍历所有的容量
            //        if(totval[i]<(totval[i-size(j)]+values[j]))
            //        {//
            //            totval[i] = totval[i - size(j)] + values[j];
            //            best[i] = j;
            //        }
            //    }
            //}

            double origAmount = 0.63;
            double toChange = origAmount;
            double remainAmount = 0.0;
            int[] coins = new int[4];
            MakeChange(origAmount,out remainAmount, ref coins);

            ShowChange(coins);

                Console.ReadLine();
        }

        public class Knapsack
        {//最优法是对应着,单位大小为1的物品,如果物品的大小不是1,而且不能分割,需要采取动态规划的方法。
            private float quantity;  //背包最大的容量
            SortedList item = new SortedList(); //存放毛毯信息的容器
            string itemList;
            public Knapsack(float max)
            {//背包最大的容量
                this.quantity = max;
            }

            public void FillSack(ArrayList objects)
            {//装包函数,前提是objects 必须是按照价钱从小到大排序的
                int pos = objects.Count - 1;
                int totalUnits = 0;
                float totalVal = 0.0F; //包内总共的价值
                int tempTot = 0;

                while (totalUnits<quantity)
                {//如果包内总共的物品大小 < 包的大小
                    tempTot += (((Carpet)objects[pos]).GetUnit());
                    if(tempTot<=quantity)
                    {//这种类型所有的毛毯都可以带走
                        totalUnits += ((Carpet)objects[pos]).GetUnit(); //数量上的增加
                        totalVal += ((Carpet)objects[pos]).GetVal();  //总价值上的增加
                        item.Add(((Carpet)objects[pos]).GetItem(), ((Carpet)objects[pos]).GetUnit()); //名字,数量
                    }
                    else
                    {//这种毛毯已经没有办法全部都装下
                        float tempUnit = quantity - totalUnits; //还可以装下的
                        float tempVal = ((Carpet)objects[pos]).ItemVal()*tempUnit;
                        totalVal += tempVal;
                        item.Add(((Carpet)objects[pos]).GetItem(), tempUnit); //名字,数量
                    }
                    pos--;
                }
            }
        }

        public class Carpet: IComparable
        {
            private string item;
            private float val; //单价
            private int unit;  //总数量

            public Carpet(string item, float val, int unit)
            {
                this.item = item;
                this.val = val;
                this.unit = unit;
            }

            public int CompareTo(object c)
            {
                return (this.val.CompareTo(((Carpet)c).val));
            }

            public int GetUnit()
            {//总数量
                return unit;
            }

            public string GetItem()
            {//商品名称
                return item;
            }

            public float GetVal()
            {//得到总价钱
                return val * unit;
            }

            public float ItemVal()
            {//得到单价
                return val;
            }
        }

        static void MakeChange(double origAmount,out double remainAmount,ref int[] coins)
        {

            remainAmount = 0.0;
            if((origAmount%0.25)<origAmount)
            {//如果origAmount>0.25
                coins[3] = (int)(origAmount / 0.25);//需要多少25美分
                remainAmount = origAmount % 0.25;
                origAmount = remainAmount;
            }

            if ((origAmount % 0.1) < origAmount)
            {//如果origAmount>0.25
                coins[2] = (int)(origAmount / 0.1);//需要多少25美分
                remainAmount = origAmount % 0.1;
                origAmount = remainAmount;
            }
            if ((origAmount % 0.05) < origAmount)
            {//如果origAmount>0.25
                coins[1] = (int)(origAmount / 0.05);//需要多少25美分
                remainAmount = origAmount % 0.05;
                origAmount = remainAmount;
            }
            if ((origAmount % 0.01) < origAmount)
            {//如果origAmount>0.25
                coins[0] = (int)(origAmount / 0.01);//需要多少25美分
                remainAmount = origAmount % 0.01;
               
            }
        }

        static void ShowChange(int[] arr)
        {
            if(arr[3]>0)
            {
                Console.WriteLine("Number of 25 :" + arr[3]);
            }
            if (arr[2] > 0)
            {
                Console.WriteLine("Number of 10 :" + arr[2]);
            }
            if (arr[1] > 0)
            {
                Console.WriteLine("Number of 5 :" + arr[1]);
            }
            if (arr[0] > 0)
            {
                Console.WriteLine("Number of 1 :" + arr[0]);
            }
        }

        static long recurFib(int n)
        {
            if(n<2)
            {
                return n;
            }
            else
            {
                return recurFib(n - 1) + recurFib(n - 2);
            }

        }

        static long iterFib(int n)
        {
            int[] val = new int[n];
            if((n==1)||(n==2))
            {//Fibnoyi数1,2都是1
                return 1;
            }
            else
            {
                val[1] = 1;
                val[2] = 1;
                for (int i = 3; i <= n - 1;i++ )
                {//注意0是第一个数,所以第n-1个数是第n
                    val[i] = val[i - 1] + val[i - 2];
                }

                return val[n - 1];
            }
        }

        public class Vertex
        {
            public bool wasVisited;
            public string label;

            public Vertex(string label)
            {
                this.label = label;
                this.wasVisited = false;
            }
        }

        public class Graph
        {
            public int NUM_VERTICES = 6;  //定点个数
            public Vertex[] vertices;
            private int[,] adjMatrix;  //边

            public int numVerts;
            public Graph(int numVertices)
            {
                NUM_VERTICES = numVertices;
                vertices = new Vertex[NUM_VERTICES];
                adjMatrix = new int[NUM_VERTICES, NUM_VERTICES];
                numVerts = 0;  //当前节点
                for (int j = 0; j < NUM_VERTICES;j++ )
                {//初始化所有的边都是不连通的
                    for (int k= 0; k < NUM_VERTICES; k++)
                    {
                        adjMatrix[j, k] = 0;
                    }
                }
            }
            //添加节点
            public void AddVertex(string strlabel)
            {
                this.vertices[numVerts] = new Vertex(strlabel);
                this.numVerts++; 
            }

            public void AddEdge(int start,int end)
            {
                adjMatrix[start, end] = 1;
                //adjMatrix[end, start] = 1;
            }

            public void ShowVertex(int v)
            {
                Console.Write(vertices[v].label + " ");
            }

            //得到没有后续的行数
            public int NoSucessor()
            {
                bool isEdge;
                for(int row=0;row<NUM_VERTICES;row++)
                {
                    isEdge = false;//默认是没有后续节点
                    for(int col=0;col<NUM_VERTICES;col++)
                    {
                        if(adjMatrix[row,col]== 1)
                        {
                            isEdge = true;
                            break;
                        }
                    }
                    if(!isEdge)
                    {//如果没有边
                        return row;
                    }
                }

                //如果所有的边都有后缀
                return -1;
            }

            //删除第vert节点,从0还是记数
            public void DelVetrix(int vert)
            {
                if(vert != NUM_VERTICES-1)
                {//如果不是最后一个节点,就需要MoveCol,MoveRow
                    for(int j = vert;j<NUM_VERTICES-1;j++)
                    {
                        vertices[j] = vertices[j + 1];
                    }

                    for (int row = vert; row < NUM_VERTICES-1;row++ )
                    {
                        MoveRow(row, NUM_VERTICES);
                    }

                    for (int col = vert; col < NUM_VERTICES-1; col++)
                    {
                        MoveCol(col, NUM_VERTICES);
                    }
                }
                NUM_VERTICES--;//如果是最后一个节点,仅仅减少个数就可以了
            }

            //使用row+1,替换row
            private void MoveRow(int row,int length)
            {
                for(int col=0;col<=length-1;col++)
                {
                    adjMatrix[row, col] = adjMatrix[row + 1, col];
                }
            }

            private void MoveCol(int col, int length)
            {
                for (int row = 0; row <= length - 1;row++ )
                {
                    adjMatrix[row, col] = adjMatrix[row, col + 1];
                }
            }

            public void TopSort()
            {
                Stack<string> gStack = new Stack<string>();  //用于反向输出
               
                while(NUM_VERTICES>0)
                {//如果还有节点
                    int currVertex = NoSucessor();  //找到一个没有后缀的节点
                    if (currVertex == -1)
                    {//说有说有节点都有后缀,就说明这个图含有“环”
                        Console.WriteLine("graph has ring.");
                        return;
                    }
                    gStack.Push(vertices[currVertex].label);
                    this.DelVetrix(currVertex); //删除这个节点
                }

                Console.WriteLine("Toplogical sorting order: ");
                while(gStack.Count>0)
                {
                    Console.Write(gStack.Pop() + ",");
                }

            }

            //找到与第v个节点连通,并且没有被访问过的节点
            private int GetAdjUnvisitedVertex(int v)
            {
                for(int j=0;j<=NUM_VERTICES-1;j++)
                {
                    if((adjMatrix[v,j]==1)&&(vertices[j].wasVisited==false))
                    {
                        return j;
                    }
                }

                return -1;
            }

            public void DepthFirstSearch()
            {
                Stack<int> gStack = new Stack<int>();
                vertices[0].wasVisited = true; //认为第一个录入的节点是图的开始

                ShowVertex(0);

                gStack.Push(0);
                int v;
                while (gStack.Count>0)
                {//如果栈里面还有元素
                    v = GetAdjUnvisitedVertex(gStack.Peek());
                    if(v == -1)
                    {//说明栈定元素周围没有没有访问过的节点了。
                        gStack.Pop();//出栈
                    }
                    else
                    {
                        vertices[v].wasVisited = true;
                        ShowVertex(v);
                        gStack.Push(v);
                    }
                }

                //再重新初始化回去
                for(int j=0;j<=NUM_VERTICES-1;j++)
                {
                    vertices[j].wasVisited = false;
                }
            }

            //最小生成树
            public void Mst()
            {
                Stack<int> gStack = new Stack<int>();
                vertices[0].wasVisited = true; //认为第一个录入的节点是图的开始

                ShowVertex(0);

                gStack.Push(0);
                int v,currv;
                while (gStack.Count > 0)
                {//如果栈里面还有元素
                    currv = gStack.Peek(); //栈顶的当前元素、
                    v = GetAdjUnvisitedVertex(gStack.Peek());
                    if (v == -1)
                    {//说明栈定元素周围没有没有访问过的节点了。
                        gStack.Pop();//出栈
                    }
                    else
                    {
                        vertices[v].wasVisited = true;
                        ShowVertex(currv);
                        ShowVertex(v);
                        Console.WriteLine();
                        gStack.Push(v);
                    }
                }

                //再重新初始化回去
                for (int j = 0; j <= NUM_VERTICES - 1; j++)
                {
                    vertices[j].wasVisited = false;
                }
            }


            public void BreadthFirstSearch()
            {
                Queue<int> gQueue = new Queue<int>();
                vertices[0].wasVisited = false;
                ShowVertex(0);

                gQueue.Enqueue(0);

                int vert1, vert2;
                while(gQueue.Count>0)
                {
                    vert1 = gQueue.Dequeue();//出栈
                    vert2 = GetAdjUnvisitedVertex(vert1);//得到vert1的相邻节点中没有被访问过的
                    while (vert2!=-1)
                    {
                        vertices[vert2].wasVisited = true;
                        ShowVertex(vert2);
                        gQueue.Enqueue(vert2);
                        vert2 = GetAdjUnvisitedVertex(vert1); //找下一个vert1的相邻节点
                    }
                }

                //重新初始化整个图
                for(int i=0;i<=NUM_VERTICES-1;i++)
                {
                    vertices[i].wasVisited = false;
                }
            }
        }

        //跳跃表
        //思路 : 从最高连层开始访问,利用这些连接,来遍历表,直到到达的数值大于要找到的数值为止,这个时候,回退到访问过的最后一个节点,并向下移动到下一层连接
        //重复相同的步骤,最终到达的连层会指引出所要查找的数值.
        //对巨大的链开销的解决方法:

        ////跳跃表节点类
        //public class SkipNode
        //{
        //    public int key;
        //    public object value;
        //    public SkipNode[] link; //用来存储指向其他节点的数组
        //    public SkipNode(int level,int key,object value)
        //    {
        //        this.key = key;
        //        this.value = value;
        //        link = new SkipNode[level];  //会指向几个其他节点
        //    }
        //}
       
        //public class SkipList
        //{
        //    private int maxLevel;  //跳跃表所允许的最大层数
        //    private int level;     //存储当前层
        //    private SkipNode header; //跳跃表的起始节点
        //    private float probalility; //存储当前链层的概率分布
        //    private const int NIL = Int32.MaxValue; //表示跳跃表末尾的特殊数的值
        //    private const int PROB = 0.5F; //层次的概率分布

        //    private void SkipList2(float probable,int maxLevel)
        //    {
        //        this.probalility = probable;
        //        this.maxLevel = maxLevel;
        //        level = 0;
        //        header = new SkipNode(maxLevel, 0, null);
        //        SkipNode nilElement = new SkipNode(maxLevel, 0, null);
        //        for(int i=0;i<=maxLevel;i++)
        //        {
        //            header.link[i] = nilElement;
        //        }
        //    }

        //    public SkipList(long maxNodes)
        //    {
        //        //概率,最大多少层
        //        this.SkipList2(PROB, (int)(Math.Ceiling(Math.Log(maxNodes) / Math.Log(1 / PROB)) - 1));
        //    }

        //    private int GenRandomLevel()
        //    {
        //        int newLevel = 0;
        //        Random r = new Random();
        //        long ran = r.NextDouble();
        //        while((newLevel<maxLevel)&&(ran<probalility))
        //        {
        //            newLevel++;
        //        }
        //        //得到一个随机的层数
        //        return newLevel;
        //    }
        //}

 

        //红黑树
        //原则如下:
        //1 树中每个节点都标记成 红色 或 黑色
        //2 把根节点标记为 黑色
        //3 如果某个节点是 红色 ,那么它的子节点必须是 黑色
        //4 从一个节点到一个叶子节点的每一条路上必须包含相同数量的黑色节点.

        //AVL树
        //public class Node : IComparable
        //{
        //    public object element;
        //    public Node left;  //左孩子
        //    public Node right; //右孩子
        //    public int height; //高度
           
        //    public Node(object data,Node lt,Node rt)
        //    {
        //        element = data;
        //        left = lt;
        //        right = rt;
        //        height = 0; //高度为0
        //    }

        //    public Node(object data)
        //    {
        //        element = data;
        //        left = null;
        //        right = null;
        //    }

        //    public int CompareTo(object obj)
        //    {
        //        return (((int)element).CompareTo((int)obj));
        //    }
           
        //    public int GetHeight()
        //    {
        //        if(this == null)
        //        {
        //            return -1;
        //        }
        //        else
        //        {
        //            return this.height;
        //        }
        //    }

        //    private Node RotateWithLeftChild(Node n2)
        //    {//对不平衡点n2的LL旋转
        //        Node n1 = n2.left;
        //        n2.left = n1.right;
        //        n1.right = n2;
        //        n2.height = Math.Max(n2.left.GetHeight(), n2.right.GetHeight()) + 1;
        //        n1.height = Math.Max(n1.left.GetHeight(), n2.GetHeight()) + 1;
        //        return n1;
        //    }

        //    private Node RotateWithRightChild(Node n1)
        //    {//对不平衡点n1的RR旋转
        //        Node n2 = n1.right;
        //        n1.right = n2.left;
        //        n2.left = n1;
        //        n1.height = Math.Max(n1.left.GetHeight(), n1.right.GetHeight()) + 1;
        //        n2.height = Math.Max(n1.GetHeight(), n2.right.GetHeight()) + 1;
        //        return n2;
        //    }

        //    private Node DoubleWithLeftChild(Node n3)
        //    {//对n3进行LR旋转, 先对n3的左孩子进行右旋转,然后对n3进行左旋转
        //        n3.left = RotateWithRightChild(n3.left);
        //        return RotateWithLeftChild(n3);
        //    }

        //    private Node DoubleWithRightChild(Node n4)
        //    {//对n4进行RL旋转,先对n4的右孩子进行
        //        n4.right = RotateWithLeftChild(n4.right);
        //        return RotateWithRightChild(n4);
        //    }
        //    private Node Insert(object item, Node n)
        //    {//在节点n上插入一个新节点,节点值为item
        //        if (n == null)
        //        {
        //            n = new Node(item, null, null);
        //        }
        //        else if (((int)item).CompareTo(((int)n.element)) < 0)
        //        {//如果要插入的节点比当前节点小,就插入到左孩子上.
        //            n.left = Insert(item, n.left); //递归的插入
        //            if (n.left.GetHeight() - n.right.GetHeight() == 2)
        //            {//如果不平衡了
        //                n = n.RotateWithLeftChild(n);
        //            }
        //            else
        //            {
        //                n = n.DoubleWithLeftChild(n);
        //            }
        //        }
        //        else if (((int)item).CompareTo(((int)n.element)) > 0)
        //        {
        //            n.right = Insert(item, n.right);
        //            if (n.right.GetHeight() - n.right.GetHeight() == 2)
        //            {//不平衡
        //                n = n.RotateWithRightChild(n);
        //            }
        //            else
        //            {
        //                n = n.DoubleWithRightChild(n);
        //            }
        //        }
        //        else
        //        {

        //        }
        //        n.height = Math.Max(n.left.GetHeight(), n.right.GetHeight()) + 1;

        //        return n;
        //    }
        //}

    }
}
       
        //快速排序,自增和自减是同时进行的
        //public int[] arr;

        //public void Swap(int item1,int item2)
        //{
        //    int temp = arr[item1];
        //    arr[item1] = arr[item2];
        //    arr[item2] = temp;
        //}

        ////一趟快排的过程
        //public int Partition(int first,int last)
        //{
        //    int pivotVal = arr[first];  //快排的分界值
        //    int theFirst = first;       //分界的位置
        //    bool okSide;
        //    first++; //first指向第二个
           
        //    do
        //    {
        //        okSide = true;  //okSize = true表示可以继续向前移动
        //        while(okSide)
        //        {
        //            if(arr[first] >pivotVal)
        //            {//不能再向前移地了
        //                okSide = false;

        //            }
        //            else
        //            {
        //                first++;
        //                okSide = (first<=last); //first必须小于last
        //            }
        //        }

        //        okSide = true;  //okSize = true表示可以继续向前移动
        //        while(okSide)
        //        {
        //            if(arr[last]<=pivotVal)
        //            {
        //                okSide = false;
        //            }
        //            else
        //            {
        //                last--;
        //                okSide = (first<=last);
        //            }
        //        }

        //        if(first<last)
        //        {//表示有交换的余地
        //            Swap(first,last);
        //            first++;
        //            last--;
        //        }

        //    } while (first<=last);

        //    //说明first和last的位置错位了
        //    Swap(theFirst,last);
        //    return last; //就是分界点的位置
        //}

        ////递归的方法来快排
        //public void RecQSort(int first,int last)
        //{
        //    if(last<=first)
        //    {
        //        return;
        //    }
        //    else
        //    {
        //        int part = this.Partition(first,last);  // 一趟快排,part为中间的位置
        //        RecQSort(first,part-1);
        //        RecQSort(part+1,last);
        //    }
        //}

        ////堆排序
        //public class Node
        //{
        //    public int data;
        //    public Node(int key)
        //    {
        //        data = key;
        //    }
        //}

        //public class Heap
        //{
        //    public Node[] heapArray = null;
        //    private int maxSize = 0;
        //    private int currSize = 0;

        //    public Heap(int maxsize)
        //    {
        //        maxSize = maxSize;
        //        heapArray = new Node[maxSize];
        //    }

        //    //插入
        //    public bool InsertAt(int pos, Node nd)
        //    {
        //        heapArray[pos] = nd;
        //        return true;
        //    }

        //    public void ShowArray()
        //    {
        //        for (int i = 0; i < currSize; i++)
        //        {
        //            if(heapArray[i]!= null)
        //            {
        //                System.Console.Write(heapArray[i].data + ",");
        //            }
        //        }
        //    }

        //    public void ShiftUp(int index)
        //    {
        //        int parent = (index - 1) / 2;
        //        Node bottom = heapArray[index];
        //        while ((index > 0) && (heapArray[parent].data < bottom.data))
        //        {//如果堆不正确了
        //            heapArray[index] = heapArray[parent];
        //            index = parent;
        //            parent = (parent - 1) / 2;
        //        }

        //        heapArray[index] = bottom;
        //    }

        //    public bool Insert(int key)
        //    {
        //        if (this.currSize == this.maxSize)
        //        {
        //            return false;
        //        }

        //        heapArray[currSize] = new Node(key);
        //        this.currSize++;
        //        return true;
        //    }

        //    //去掉堆顶的元素
        //    public Node Remove()
        //    {
        //        Node root = heapArray[0];
        //        heapArray[0] = heapArray[currSize];  //把最后一个元素弄到第一个的位置上。
        //        currSize--;
        //        ShiftDown(0); //把第一个元素向下调整,用来形成一个正确的堆
        //        return root;
        //    }

        //    public void ShiftDown(int index)
        //    {
        //        int largeChild;//
        //        Node top = heapArray[index];//要下移动的节点
        //        while (index < (int)(currSize / 2))
        //        {//如果还需要向下移动
        //            int leftChild = 2 * index + 1; //左孩子的位置
        //            int rightChild = leftChild + 1;     //右孩子的位置

        //            if ((rightChild < currSize) && (heapArray[leftChild].data < heapArray[rightChild].data))
        //            {//如果右孩子存在,并且右孩子比较大,右孩子存在,当然左孩子一定存在
        //                largeChild = rightChild;
        //            }
        //            else
        //            {
        //                largeChild = leftChild;
        //            }

        //            if (top.data > heapArray[largeChild].data)
        //            {
        //                break;//这个已经是一个堆了
        //            }
        //            heapArray[index] = heapArray[largeChild];
        //            index = largeChild;
        //        }

        //        heapArray[index] = top;
        //    }
        //}

//        //归并排序

//        //希尔排序: 对远距离而非相邻的数据项进行比较,当算法循环遍历数据集合的时候,每个数据项间的距离会缩短,直到算法对相邻数据项进行比较时候才终止。
//        //static public void ShellSort(int[] arr)
//        //{
//        //    int numElement = arr.Length;
//        //    int inner, temp;
//        //    int h = 3;  //间距首先从3开始

//        //    while(h>0)
//        //    {//h=1.最后的间距肯定要变成1
//        //        for(int outer=h;outer<=numElement-1;outer++)
//        //        {//numElement-1 是数组的最后一个元素,一个一个元素的过
//        //            temp = arr[outer];
//        //            inner = outer;
//        //            while((inner>h-1)&&(arr[inner-h]>=temp))
//        //            {//如果inner-h是一个有效的索引,并且元素的顺序不对,就换
//        //                arr[inner] = arr[inner - h];
//        //                inner = inner - h; //inner指向前一个元素
//        //            }

//        //            arr[inner] = temp; //一趟结束

//        //        }
//        //        h = (h - 1) % 3;
//        //    }

//        //    for (int i = 0; i <= arr.GetUpperBound(0); i++)
//        //    {
//        //        Console.Write(arr[i].ToString() + ",");
//        //    }
//        //}
       
       
//        //public class CSet
//        //{
//        //    private BitArray data;
//        //    public CSet()
//        //    {//可以存储五个数字
//        //        data = new BitArray(5);
//        //    }

//        //    public void Add(int item)
//        //    {//item表示位置
//        //        data[item] = true;
//        //    }

//        //    public bool IsMember(int item)
//        //    {
//        //        return data[item]; //判断item是否为真
//        //    }
           
//        //    public void Remove(int item)
//        //    {
//        //        data[item] = false;
//        //    }

//        //    public CSet Union(CSet aSet)
//        //    {
//        //        CSet tempSet = new CSet();
//        //        for(int i=0;i<=data.Count-1;i++)
//        //        {
//        //            tempSet.data[i] = (this.data[i]) || (aSet.data[i]);
//        //        }

//        //        return tempSet;
//        //    }

//        //    public CSet Intersection(CSet aSet)
//        //    {
//        //        CSet tempSet = new CSet();
//        //        for (int i = 0; i <= data.Count - 1; i++)
//        //        {
//        //            tempSet.data[i] = (this.data[i]) && (aSet.data[i]);
//        //        }

//        //        return tempSet;
//        //    }

//        //    public CSet Difference(CSet aSet)
//        //    {
//        //        CSet tempSet = new CSet();
//        //        for (int i = 0; i <= data.Count - 1; i++)
//        //        {
//        //            tempSet.data[i] = (this.data[i]) && !(aSet.data[i]);
//        //        }

//        //        return tempSet;
//        //    }

//        //    public bool IsSubset(CSet aSet)
//        //    {
//        //        CSet tempSet = new CSet();
//        //        for(int i=0;i<=data.Count-1;i++)
//        //        {
//        //            if(this.data[i]&&!aSet.data[i])
//        //            {//如果某位在this中,但是不在aSet中,就说明this不是aSet的子集合
//        //                return false;
//        //            }
//        //        }

//        //        return true;
//        //    }

//        //    public override string ToString()
//        //    {
//        //        string s = "";
//        //        for (int i = 0; i <= data.Count - 1;i++ )
//        //        {
//        //            if(data[i])
//        //            {
//        //                s += i;
//        //            }
//        //        }

//        //        return s;
//        //    }
//        //}

//        //public class CSet
//        //{
//        //    private Hashtable data;
//        //    public CSet()
//        //    {
//        //        data = new Hashtable();
//        //    }

//        //    public void Add(object item)
//        //    {
//        //        if(!data.ContainsValue(item))
//        //        {//如果HashTable中没有含有item
//        //            data.Add(Hash(item), item);
//        //        }
//        //    }

//        //    private string Hash(object item)
//        //    {
//        //        char[] chars;
//        //        string s = item.ToString();
//        //        int hashValue = 0;
//        //        chars = s.ToCharArray();
//        //        for(int i=0;i<chars.GetUpperBound(0);i++)
//        //        {
//        //            hashValue += (int)chars[i];
//        //        }

//        //        return hashValue.ToString();
//        //    }

//        //    public void Remove(object item)
//        //    {
//        //        data.Remove(Hash(item));
//        //    }

//        //    public int Size()
//        //    {
//        //        return data.Count;
//        //    }

//        //    public CSet Union(CSet aSet)
//        //    {//并
//        //        CSet tempSet = new CSet();
//        //        foreach(object hashObject in data.Keys)
//        //        {//把this中所有的数据项弄到tempSet中
//        //            tempSet.Add(this.data[hashObject]);
//        //        }

//        //        foreach(object hashObject in aSet.data.Keys)
//        //        {//所有aSet中的内容
//        //            if(!this.data.ContainsKey(hashObject))
//        //            {//如果没有找到,添加到tempSet中
//        //                tempSet.Add(aSet.data[hashObject]);
//        //            }
//        //        }

//        //        return tempSet;
//        //    }

//        //    public CSet Intersection(CSet aSet)
//        //    {//交
//        //        CSet tempSet = new CSet();
//        //        foreach (object hashObject in this.data.Keys)
//        //        {//得到本身所有的key
//        //            if(aSet.data.ContainsKey(hashObject))
//        //            {//如果也在aSet中
//        //                tempSet.Add(aSet.data[hashObject]);
//        //            }
//        //        }

//        //        return tempSet;
//        //    }

//        //    //判断 this 是否是 aSet的子集合
//        //    public bool Subset(CSet aSet)
//        //    {
//        //        if(this.Size()>aSet.Size())
//        //        {
//        //            return false;
//        //        }
//        //        else
//        //        {//看this的所有元素是不是在aSet中
//        //            foreach(object key in this.data.Keys)
//        //            {
//        //                if(!aSet.data.Contains(key))
//        //                {//如果有一个不包含就是false
//        //                    return false;
//        //                }
//        //            }
//        //        }

//        //        return true;

//        //    }

//        //    public CSet Difference(CSet aSet)
//        //    {
//        //        CSet tempSet = new CSet();
//        //        foreach(object hashObject in data.Keys)
//        //        {//对于所有的在data中的key
//        //            if(!aSet.data.Contains(hashObject))
//        //            {//如果不在aSet中
//        //                tempSet.Add(data[hashObject]);
//        //            }
//        //        }

//        //        return tempSet;
//        //    }

//        //    public override string ToString()
//        //    {
//        //        string s = "";
//        //        foreach(object key in data.Keys)
//        //        {
//        //            s += data[key] + ",";
//        //        }
//        //        return s;
//        //    }
//        //}

//        //public void InOrder(Node theRoot)
//        //{
//        //    if(theRoot != null)
//        //    {
//        //        InOrder(theRoot.left);
//        //        theRoot.DisplayNode();
//        //        InOrder(theRoot.right);
//        //    }
//        //}

//        //public class Node
//        //{
//        //    public int Data;
//        //    public Node left;
//        //    public Node right;
//        //    public void DisplayNode()
//        //    {
//        //        Console.WriteLine(Data);
//        //    }
//        //}

//        //public class BinarySearchTree
//        //{
//        //    public Node root;
//        //    public BinarySearchTree()
//        //    {
//        //        root = null;
//        //    }

//        //    public void InOrder(Node theRoot)
//        //    {
//        //        if (theRoot != null)
//        //        {
//        //            InOrder(theRoot.left);
//        //            theRoot.DisplayNode();
//        //            InOrder(theRoot.right);
//        //        }
//        //    }

//        //    public void insert(int i)
//        //    {
//        //        Node newNode = new Node();
//        //        newNode.Data = i;

//        //        if(root == null)
//        //        {
//        //            root = newNode;
//        //        }
//        //        else
//        //        {
//        //            Node current = root;
//        //            Node parent;
//        //            while(true)
//        //            {
//        //                parent = current;
//        //                if (i < current.Data)
//        //                {
//        //                    current = current.left;
//        //                    if(current == null)
//        //                    {
//        //                        parent.left = newNode;
//        //                        break;
//        //                    }
//        //                }
//        //                else
//        //                {
//        //                    current = current.right;
//        //                    if(current == null)
//        //                    {
//        //                        parent.right = newNode;
//        //                        break;
//        //                    }
//        //                }
//        //            }
//        //        }
//        //    }

//        //    public int FindMin()
//        //    {
//        //        Node current = root;
//        //        while(current.left != null)
//        //        {
//        //            current = current.left;
//        //        }
//        //        return current.Data;
//        //    }

//        //    public int FindMax()
//        //    {
//        //        Node current = root;
//        //        while(current.right != null)
//        //        {
//        //            current = current.right;
//        //        }

//        //        return current.Data;
//        //    }

//        //    public Node Find(int key)
//        //    {
//        //        Node current = root;
//        //        while(current.Data != key)
//        //        {
//        //            if(key<current.Data)
//        //            {
//        //                current = current.left;
//        //            }
//        //            else
//        //            {
//        //                current = current.right;
//        //            }

//        //            if(current == null)
//        //            {
//        //                return null;
//        //            }
//        //        }

//        //        return current;
//        //    }

//        //    public bool Delete(int key)
//        //    {
//        //        Node current = root;
//        //        Node parent = root;
//        //        bool isLeftChild = true;

//        //        while(current.Data != key)
//        //        {
//        //            parent = current;
//        //            if(key < current.Data)
//        //            {
//        //                isLeftChild = true;
//        //                current = current.left;
//        //            }
//        //            else
//        //            {
//        //                isLeftChild = false;
//        //                current = current.right;
//        //            }

//        //            if(current == null)
//        //            {
//        //                return false; //删除叶子节点失败
//        //            }
//        //        }

//        //        if ((current.left == null) && (current.right == null))
//        //        {//如果是叶子节点
//        //            if(current == root)
//        //            {
//        //                root = null;
//        //            }
//        //            else if (isLeftChild)
//        //            {
//        //                parent.left = null;
//        //            }
//        //            else
//        //            {
//        //                parent.right = null;
//        //            }
//        //        }
//        //        return true;
//        //    }

//        //    public Node GetSuccessor(Node delNode)
//        //    {//delNode是要删除的节点,delNode有左右两个子树
//        //        Node successorParent = delNode;
//        //        Node successor = delNode;
//        //        Node current = delNode.right; //current是要删除节点的右孩子
//        //        while(current!=null)
//        //        {//一直找到右孩子中最小的那个节点
//        //            successorParent = current;
//        //            successor = current;
//        //            current = current.left;
//        //        }

//        //        if(successor != delNode.right)
//        //        {//如果找到的右孩子中最小的不是delnode的直接右孩子
//        //            successor.right = delNode.right;
//        //            successorParent.left = successor.right;
//        //        }

//        //        return successor; //
//        //    }
//        //}

//        //public class Node
//        //{
//        //    public object Element;
//        //    public Node Link;
//        //    public Node()
//        //    {
//        //        Element = null;
//        //        Link = null;
//        //    }

//        //    public Node(object theElement)
//        //    {
//        //        Element = theElement;
//        //        Link = null;
//        //    }
//        //}

 

//        //class InsertBeforeHeaderException: ApplicationException
//        //{
//        //    public InsertBeforeHeaderException(string message):base(message)
//        //    {

//        //    }
//        //}

//        //public class ListIter
//        //{
//        //    private Node current;
//        //    private Node previous;
//        //    LinkList theList;

//        //    public ListIter(LinkList list)
//        //    {
//        //        theList = list;
//        //        current = theList.GetFirst();
//        //        previous = null;
//        //    }

//        //    public void NextLink()
//        //    {
//        //        previous = current;
//        //        current = current.Link;
//        //    }

//        //    public Node GetCurrent()
//        //    {
//        //        return current;
//        //    }

//        //    public void InsertBefore(object theElement)
//        //    {
//        //        Node newNode = new Node(theElement);
//        //        if(previous.Link == null)
//        //        {
//        //            throw new InsertBeforeHeaderException("can not insert here");
//        //        }
//        //        else
//        //        {
//        //            newNode.Link = previous.Link;
//        //            previous.Link = newNode;
//        //            current = newNode;
//        //        }
//        //    }

//        //    public void insertAfter(object theElement)
//        //    {
//        //        Node newNode = new Node(theElement);
//        //        newNode.Link = current.Link;
//        //        current.Link = newNode;
//        //        NextLink();
//        //    }

//        //    public void Remove()
//        //    {
//        //        previous.Link = current.Link;
//        //    }

//        //    public void Reset()
//        //    {
//        //        current = theList.getFirst();
//        //        previous = null;
//        //    }

//        //    public bool AtEnd()
//        //    {
//        //        return (current.Link == null);
//        //    }
//        //}

//        //public class LinkList
//        //{
//        //    protected Node header;
           
//        //    public LinkList()
//        //    {
//        //        header = new Node("header");
//        //    }

//        //    public Node GetFirst()
//        //    {
//        //        return header;
//        //    }

//        //    public bool IsEmpty()
//        //    {
//        //        return (header.Link == null);
//        //    }

//        //    public void ShowList()
//        //    {
//        //        Node current = header.Link;
//        //        while(current != null)
//        //        {
//        //            Console.WriteLine(current.Element);
//        //            current = current.Link;
//        //        }
//        //    }

//        //    public Node getFirst()
//        //    {
//        //        return header;
//        //    }
//        //}

//        //static void ShowDistrib(string[] arr)
//        //{
//        //    for(int i=0;i<=arr.GetUpperBound(0);i++)
//        //    {
//        //        if(arr[i]!=null)
//        //        {
//        //            Console.WriteLine(i + ":" + arr[i]);
//        //        }
//        //    }
//        //}

////        //得到s的哈希码
////        //s
////        //arr 要存储的数组
////        static int SimpleHash(string s, string[] arr)
////        {
////            int tot = 0;
////            char[] cname;
////            cname = s.ToCharArray(); //string -> char[]
////            for(int i=0;i<=cname.GetUpperBound(0);i++)
////            {//得到所有char的Ascii码的和
////                tot += (int)cname[i];
////            }
////            return tot % arr.GetUpperBound(0);
////        }

////        //Horner
////        static int BetterHash(string s, string[] arr)
////        {
////            long tot = 0;
////            char[] cname;
////            cname = s.ToCharArray(); //string -> char[]
////            for (int i = 0; i <= cname.GetUpperBound(0); i++)
////            {//得到所有char的Ascii码的和
////                tot += 37 * tot + (int)cname[i];
////            }
           
////            tot =tot % arr.GetUpperBound(0);
////            if(tot<0)
////            {
////                tot += arr.GetUpperBound(0);
////            }
////            return (int)tot;
////        }

////        public class BucketHash
////        {
////            private const int SIZE = 101;
////            ArrayList[] data;
////            public BucketHash()
////            {
////                data = new ArrayList[SIZE];
////                for(int i=0;i<=SIZE-1;i++)
////                {
////                    data[i] = new ArrayList(4);
////                }
////            }

////            public int Hash(string s)
////            {//求s的hash数值
////                long tot = 0;
////                char[] charray;
////                charray = s.ToCharArray();
////                for(int i=0;i<=s.Length-1;i++)
////                {
////                    tot += 37 * tot + (int)charray[i];
////                }

////                tot = tot % data.GetUpperBound(0);
////                if(tot<=0)
////                {
////                    tot += data.GetUpperBound(0);
////                }
////                return (int)tot;
////            }

////            public void Insert(string item)
////            {
////                int hash_value;
////                hash_value = Hash(item);
////                if(!data[hash_value].Contains(item))
////                {
////                    data[hash_value].Add(item);
////                }
////            }

////            public void Remove(string item)
////            {
////                int hash_value;
////                hash_value = Hash(item);
////                if(data[hash_value].Contains(item))
////                {
////                    data[hash_value].Remove(item);
////                }
////            }
////        }

////        public class IPAddress : DictionaryBase
////        {
////            public IPAddress()
////            {

////            }

////            public IPAddress(string txtFile)
////            {
////                string line;
////                string[] words;
////                StreamReader inFile;
////                inFile = File.OpenText(txtFile);
////                while (inFile.Peek()!= -1)
////                {
////                    line = inFile.ReadLine();
////                    words = line.Split(',');
////                    this.InnerHashtable.Add(words[0], words[1]);
////                }

////                inFile.Close();
////            }

////            public void Add(string name,string ip)
////            {
////                base.InnerHashtable.Add(name, ip);
////            }

////            public string Item(string name)
////            {
////                return base.InnerHashtable[name].ToString();
////            }

////            public void Remove(string name)
////            {
////                base.InnerHashtable.Remove(name);
////            }
////        }

////        static void BuildSB(int size)
////        {
////            StringBuilder sbObject = new StringBuilder();
////            for(int i=0;i<=size;i++)
////            {
////                sbObject.Append("a");
////            }
////        }

////        static void BuildString(int size)
////        {
////            string stringObject = "";
////            for(int i=0;i<=size;i++)
////            {
////                stringObject += "a";
////            }
////        }
////        static void showName(string[] arr)
////        {
////            char[] charArr = new char[]{' '};
////            for(int i=0;i<=arr.GetUpperBound(0);i++)
////            {
////                arr[i] = arr[i].Trim(charArr[0]);
////                arr[i] = arr[i].TrimEnd(charArr[0]);
////                Console.Write(arr[i]);
////            }
////        }

////        static ArrayList SplitWords(string astring)
////        {
////            string[] ws = new string[astring.Length - 1];
////            ArrayList words = new ArrayList();
////            int pos;
////            string word;

////            pos = astring.IndexOf(" ");
////            while(pos>0)
////            {//如果可以找到空格
////                word = astring.Substring(0, pos);
////                words.Add(word);
////                astring = astring.Substring(pos + 1, astring.Length - pos - 1);

////                pos = astring.IndexOf(" ");
////            }

////            words.Add(astring.Substring(0));

////            return words;
////        }

////        enum DigitType{ ones=1,tens=10}
////        static void DisplayArray(int[] n)
////        {
////            for(int x=0;x<=n.GetUpperBound(0);x++)
////            {
////                Console.Write(n[x] + " ");
////            }
////            Console.WriteLine("");
////        }

////        static void RSort(Queue[] que,int[] n,DigitType digit)
////        {//发散的过程
////            int snum;
////            for(int x=0;x<=n.GetUpperBound(0);x++)
////            {
////                if(digit == DigitType.ones)
////                {
////                    snum = n[x] % 10;
////                }
////                else
////                {
////                    snum = n[x] / 10;
////                }

////                que[snum].Enqueue(n[x]);
////            }
////        }

////        static void BuildArray(Queue[] que,int[] n)
////        {
////            int y = 0;
////            for(int x=0;x<=9;x++)
////            {
////                while(que[x].Count>0)
////                {
////                    n[y] = Int32.Parse(que[x].Dequeue().ToString());
////                    y++;
////                }
////            }
////        }

////        static void newDancers(Queue male, Queue female)
////        {//找一对跳舞的
////            Dancer m, w;
////            m = new Dancer();
////            w = new Dancer();
////            if(male.Count>0&& female.Count>0)
////            {//如果男女队列都有人
////                m.GetName(male.Dequeue().ToString());
////                w.GetName(female.Dequeue().ToString());
////            }
////            else if((male.Count>0)&&(female.Count == 0))
////            {//没有女人了
////                Console.WriteLine("Waiting on a female dancer");
////            }else if((female.Count>0)&&(male.Count==0))
////            {//没有男人了
////                Console.WriteLine("Waiting on a male dancer");
////            }

////        }

////        static void MulBase(int n,int b)
////        {
////            Stack Digits = new Stack();
////            do
////            {
////                Digits.Push(n % b);
////                n = n / b;
////            } while (n != 0);

////            while(Digits.Count>0)
////            {
////                Console.Write(Digits.Pop());
////            }

////            Console.WriteLine();
////        }

////        static bool IsNumberic(string input)
////        {
////            bool flag = true;
////            string pattern = (@"^/d+$");
////            Regex validate = new Regex(pattern);
////            if(!validate.IsMatch(input))
////            {//如果不是数字
////                flag = false;
////            }
////            return flag;
////        }

////        static void Calculate(Stack N,Stack O,string exp)
////        {
////            string ch, token = "";
////            for (int p = 0; p < exp.Length;p++ )
////            {
////                ch = exp.Substring(p, 1);
////                if(IsNumberic(ch))
////                {//如果是数字
////                    token += ch;
////                }
////                if(ch==" "||p==(exp.Length-1))
////                {
////                    N.Push(token);
////                    token = "";
////                }
////                else if (ch == "+" || ch == "-" || ch == "*" || ch == "/")
////                {
////                    O.Push(ch);
////                }

////                if(N.Count == 2)
////                {
////                    Compute(N, O);
////                }
////            }
////        }
       
////        static void Compute(Stack N,Stack O)
////        {
////            int oper1, oper2;
////            string oper;
////            oper1 = Convert.ToInt32(N.Pop());
////            oper2 = Convert.ToInt32(N.Pop());
////            oper = Convert.ToString(O.Pop());
 
////            switch (oper)
////            {
////                case "+":
////                    N.Push(oper1 + oper2);
////                    break;
////                case "-":
////                    N.Push(oper1 - oper2);
////                    break;
////                case "*":
////                    N.Push(oper1 * oper2);
////                    break;
////                case "/":
////                    N.Push(oper1 / oper2);
////                    break;
////            }
////        }

////        static int SeqSearch(int[] arr,int sValue)
////        {
////            for(int index=0;index<arr.Length;index++)
////            {
////                if(arr[index] == sValue)
////                {
////                    swap(arr, index, index-1);
////                    return index;
////                }
////            }
////            return -1;
////        }

////        static void swap(int[] arr,int item1,int item2)
////        {
////            int temp = arr[item1];
////            arr[item1] = arr[item2];
////            arr[item2] = temp;
////        }

////        static int sumNums(params int[] nums)
////        {
////            int sum = 0;
////            for(int i=0;i<=nums.GetUpperBound(0);i++)
////            {
////                sum += nums[i];
////            }
////            return sum;
////        }

////        TimeSpan getCostTime(DateTime startTime)
////        {
////            TimeSpan endTime;
////            endTime = DateTime.Now.Subtract(startTime);
////            return endTime;
////        }
       
////        static void DisplayNums(int[] arr)
////        {
////            for(int i=0;i<=arr.GetUpperBound(0);i++)
////            {
////                Console.Write(arr[i] + " ");
////            }
////        }

////        static void BuildArray(int[] arr)
////        {
////            for(int i=0;i<=9999;i++)
////            {
////                arr[i] = i;
////            }
////        }

////        static void Swap<T>(ref T val1, ref T val2)
////        {
////            T temp;
////            temp = val2;
////            val2 = val1;
////            val1 = temp;
////        }
////    }

////    public class Timing
////    {
////        TimeSpan startingTime;
////        TimeSpan duration;
////        public Timing()
////        {
////            startingTime = new TimeSpan(0);
////            duration = new TimeSpan(0);
////        }

////        public void startTime()
////        {
////            GC.Collect();
////            GC.WaitForPendingFinalizers();
////            startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
////        }
////        public void StopTime()
////        {
////            duration = Process.GetCurrentProcess().Threads[0].UserProcessorTime.Subtract(startingTime);

////        }

////        public TimeSpan Result()
////        {
////            return duration;
////        }
////    }

public class Timing2
{
    TimeSpan duration;
    public Timing2()
    {
        duration = new TimeSpan(0);
    }

    public void startTime()
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    public void stopTime()
    {
        duration = Process.GetCurrentProcess().TotalProcessorTime;
    }

    public TimeSpan Result()
    {
        return duration;
    }
}
////    //public class Node<T>
////    //{
////    //    T data;
////    //    Node<T> link;

////    //    public Node(T data,Node<T> link)
////    //    {
////    //        this.data = data;
////    //        this.link = link;
////    //    }
////    //}

////    public struct Name
////    {
////        private string fname, mname, lname;
////        public Name(string first, string middle, string last)
////        {
////            fname = first;
////            mname = middle;
////            lname = last;
////        }

////        public string firstName
////        {
////            get
////            {
////                return fname;
////            }

////            set
////            {
////                fname = value;
////            }
////        }

////        public string middleName
////        {
////            get
////            {
////                return mname;
////            }
////            set
////            {
////                mname = value;
////            }
////        }

////        public string lastName
////        {
////            get
////            {
////                return lname;
////            }
////            set
////            {
////                lname = value;
////            }
////        }

////        public override string ToString()
////        {
////            return (String.Format("{0} {1} {2}", fname, mname, lname));
////        }

////        public string Initials()
////        {
////            return (String.Format("{0}{1}{2}",fname.Substring(0,1),mname.Substring(0,1),lname.Substring(0,1)));
////        }
////    }

////    public class Collection : CollectionBase
////    {
////        public void Add(Object item)
////        {
////            this.InnerList.Add(item);
////        }

////        public void Remove(Object item)
////        {
////            this.InnerList.Remove(item);
////        }

////        public void Clear()
////        {
////            this.InnerList.Clear();
////        }

////        public int Count()
////        {
////            return this.InnerList.Count;
////        }
////    }
////}

//////插入排序
//////选择排序
//////冒泡排序

////class CStack
////{
////    private int p_index;
////    private ArrayList list;
////    public CStack()
////    {
////        list = new ArrayList();
////        p_index = -1;
////    }

////    public int count
////    {
////        get
////        {
////            return list.Count;
////        }
////    }

////    public void push(object item)
////    {
////        list.Add(item);
////        p_index++;
////    }

////    public object pop()
////    {
////        object obj = list[p_index];
////        list.RemoveAt(p_index);
////        p_index--;
////        return obj;
////    }

////    public void clear()
////    {
////        list.Clear();
////        p_index = -1;
////    }

////    public object peek()
////    {
////        return list[p_index];
////    }
////}

////public class CArray
////{
////    private int[] arr;
////    private int upper;
////    private int numElements;

////    public CArray(int size)
////    {
////        arr = new int[size];
////        upper = size - 1;
////        numElements = 0;
////    }

////    public int RbinSearch(int value,int lower,int upper)
////    {
////        if(lower> upper)
////        {
////            return -1;
////        }
////        else
////        {
////            int mid;
////            mid = (lower + upper) / 2;
////            if(value<arr[mid])
////            {
////                return RbinSearch(value, lower, mid - 1);
////            }
////            else if(value>arr[mid])
////            {
////                return RbinSearch(value, mid + 1, upper);
////            }
////            else
////            {
////                return mid;
////            }

////        }
////    }
   
////    public int binSearch(int value)
////    {
////        int upperBound, lowerBound, mid;
////        upperBound = arr.Length - 1;
////        lowerBound = 0;

////        while (lowerBound <= upperBound)
////        {
////            mid = (upperBound + lowerBound) / 2;
////            if (arr[mid] == value)
////            {
////                return mid;
////            }
////            else
////            {
////                if (value < arr[mid])
////                {
////                    upperBound = mid - 1;
////                }
////                else
////                {
////                    lowerBound = mid + 1;
////                }
////            }
////        }

////        return -1;
////    }

////    public void Insert(int item)
////    {
////        arr[numElements] = item;
////        numElements++;
////    }

////    public void DisplayElements()
////    {
////        for (int i = 0; i <= upper;i++ )
////        {
////            Console.Write(arr[i] + " ");
////        }
////        Console.WriteLine("");
////    }

////    public void Clear()
////    {
////        for (int i = 0; i <= upper;i++ )
////        {
////            arr[i] = 0;
////        }

////        numElements = 0;
////    }

////    public void BubbleSort()
////    {
////        int temp;
////        for (int outer = upper; outer >= 1;outer-- )
////        {
////            for (int inner = 0; inner <= outer - 1;inner++ )
////            {
////                if((int)arr[inner]>(int)arr[inner+1])
////                {
////                    temp = arr[inner];
////                    arr[inner] = arr[inner + 1];
////                    arr[inner + 1] = temp;
////                }
////            }
////            //this.DisplayElements();
////        }
////    }

////    public void SelectionSort()
////    {
////        int min, temp;
////        for(int outer=0;outer<=upper;outer++)
////        {
////            min = outer;
////            for (int inner = outer + 1; inner <= upper;inner++ )
////            {
////                if(arr[inner]<arr[min])
////                {
////                    min = inner;
////                }
////            }

////            temp = arr[outer];
////            arr[outer] = arr[min];
////            arr[min] = temp;
////            //this.DisplayElements();
////        }
//    }
//}

////public class CQueue
////{
////    private ArrayList pqueue;
////    public CQueue()
////    {
////        pqueue = new ArrayList();
////    }

////    public void EnQueue(object item)
////    {
////        pqueue.Add(item);
////    }

////    public void Dequeue()
////    {
////        pqueue.RemoveAt(0);
////    }

////    public object Peek()
////    {
////        return pqueue[0];
////    }

////    public void ClearQueue()
////    {
////        pqueue.Clear();
////    }

////    public int Count()
////    {
////        return pqueue.Count;
////    }

////}

////public struct Dancer
////{
////    public string name;
////    public string sex;
   
////    public void GetName(string n)
////    {
////        name = n;
////    }

////    public override string ToString()
////    {
////        return name;
////    }
////}

////public struct pqItgem
////{
////    public int priority;
////    public string name;
////}

 

 

 

 

 

 

 

 

你可能感兴趣的:(数据结构和算法 - C#语言描述)