经典代码收集

   /// <summary>

    /// 数学界把这个数列叫做斐波那契数列

    /// 112358132134...... 求第n位数是多少

    /// n 由用户输入

    /// </summary>

    class Program

    {

        static void Main(string[] args)

        {

            string s = Console.ReadLine();

            int num;

            if (int.TryParse(s, out num))

                Console.WriteLine("结果是: {0}", countFibonacci(num));

            else

                Console.WriteLine("无效的输入...");

            Console.ReadLine();

       

        static int countFibonacci(int n)

        {

           if (n < 2)

                return n;

            return countFibonacci(n - 1) + countFibonacci(n - 2);

        }

        //countFibonacci(n - 1)求出它的前一个数

        //countFibonacci(n - 2)求出它的前的第二个数

    }

        /// <summary>

        /// 字符串反转的算法

        /// </summary>

        /// <param name="str"></param>

        /// <returns></returns>

        public static string Reverse(string str)

        {

            if (string.IsNullOrEmpty(str))

            {

                throw new ArgumentException("参数不合法");

            }

            //当使用StringBuilder时,请注意,应在构造StringBuilder对象时指明初始容量,

            //否则默认容量是16个字符,当由于追加字符而超出默认容量时,

            //就会分配一个新的串缓冲区,大小是原缓冲区的两倍

            StringBuilder sb = new StringBuilder(str.Length);

 

            for (int index = str.Length - 1; index >= 0; index--)

            {

                sb.Append(str[index]);

            }

            return sb.ToString();

        } 

 

 

 

    /// <summary>

    /// 1 加到 100 C#算法

    /// </summary>

    class Program

    {

        static void Main(string[] args)

        {

            int Num = 100;

            int Sum = 0;

            for (int i = 1, j = Num; i <= Num / 2; i++, j--)

            {

                Sum += i + j;

            }

            Console.WriteLine(Sum.ToString());

            Console.ReadLine();

        }

    }

 

 

    /// <summary>

    /// 1 100 的素数

    /// 素数:是这样的整数,它除了能表示为它自己和1的乘积以外,

    /// 不能表示为任何其它两个整数的乘积

    /// </summary>

    class Program

    {

        static void Main(string[] args)

        {

         Console.Write(2 + " ");

         for (int m = 3; m <= 100; m+=2)

         {

             bool a = true;

             if (m%2!=0)

             {

                for (int i = 3; i < m/2; i+=2)

                {

                    if (m%i==0)

                    { a = false; break; }

                }

            }

            if (a == true)

            {

                Console.Write(m.ToString() + " ");

            }

        }

        Console.ReadLine();

        }

    } 

posted @ 2010-03-04 11:11  火星贝贝  阅读(341)  评论(0编辑  收藏  举报