常见的程序设计题

1 斐波那契数列

1.1 题目:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

 //斐波那契数列
        static void Main(string[] args)
        {
            for (int i = 1; i <= 12; i++)
            {
                Console.WriteLine(Foo(i));
            }
            Console.Read();
        }

        public static int Foo(int i)
        {
            if (i < 0)
                return 0;
            else if (i>0 &&i <= 2)//第一个月,第二个月兔子的总对数是1对
                return 1;
            else
                return Foo(i - 1) + Foo(i - 2);//第三月开始生1对新兔子(总数:2对),第四月时新生1对兔子(2+1=3对),第五月时新生两对兔子(3+2=5),第六月时新生3对兔子(5+3=8)
        }

 

2 判断素数

2.1 题目:判断101-200之间有多少个素数,并输出所有素数。

        //101-200之间的素数
        static void Main(string[] args)
        {
            int count = 0;
            for (int i = 101; i < 200; i++)
            {
                bool b = true;//默认此数为素数
                for (int j = 2; j <= Math.Sqrt(i); j++)
                {
                    if (i % j == 0)//能被2到该数开方直接的数整除 则不是素数
                    {
                        b = false;
                        break;
                    }
                }
                if (b)
                {
                    count++;
                    Console.WriteLine(i);
                }
            }
            Console.WriteLine("101-200之间的素数个数:" + count);
            Console.Read();
        }

3 水仙花数

3.1题目:打印出所有的"水仙花数(narcissus number)",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

        //所有的"水仙花数(narcissus number)" 
        static void Main(string[] args)
        {
            int count = 0;
            for (int i = 100; i < 1000; i++)
            {
                var num = NarcissusNum(i);
                if (num != 0)
                {
                    count++;
                    Console.WriteLine(num);
                }
            }
            Console.WriteLine("100-1000之间的水仙花个数:" + count);
            Console.Read();
        }

        public static int NarcissusNum(int num)
        {
            int b = num / 100;//百位数
            int s = (num % 100) / 10;//十位数
            int g = (num % 100) % 10;//各位数

            if (b * b * b + s * s * s + g * g * g == num)//百位数+十位数+个位数之和等于该数即为水仙花数
                return num;
            else
                return 0;
        }

 

4 杨辉三角

 题目:打印出杨辉三角形(要求打印出10行如下图)  
        1 
        1 1 
       1 2 1 
       1 3 3 1 
      1 4 6 4 1 
     1 5 10 10 5 1

  //等腰杨辉三角
        static void Main(string[] args)
        {
            int n;
            Console.Write("输入n:");
            n = int.Parse(Console.ReadLine());
            if (n > 13)
            {
                Console.WriteLine("输入的数值太大!");
            }
            else
            {
                int i;
                int[] a = new int[n];
                for (i = 0; i < n; i++)
                {
                    a[i] = 1;
                    for (int j = i - 1; j > 0; j--)
                        a[j] = a[j - 1] + a[j];
                    for (int k = n; k >= i; k--)
                        Console.Write("  ");
                    for (int j = 0; j <= i; j++)
                    {
                        string s = a[j].ToString();
                        if (s.Length == 1)
                            Console.Write(a[j] + "   ");
                        else
                            Console.Write(a[j] + "  ");
                    }
                    Console.WriteLine();
                }
            }
            Console.Read();
        }

6,产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复

 static void Main(string[] args)
        {
            var array = InsertRandomNum();
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine(array[i]);
            }
            Console.Read();
        }

        //产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复
        public static int[] InsertRandomNum()
        {
            int[] array = new int[100];
            Random random = new Random();
            List<int> list = new List<int>();

            while (list.Count < 100)
            {
                int num = random.Next(1, 101);

                if (!list.Contains(num))
                {
                    list.Add(num);
                }
            }

            for (int i = 0; i < list.Count; i++)
            {
                array[i] = list[i];
            }

            return array;
        }

 

posted on 2014-11-26 11:31  lihfei89  阅读(223)  评论(0编辑  收藏  举报

导航