.NET面试常见题目

来源:互联网 发布:床垫 独立弹簧 知乎 编辑:程序博客网 时间:2024/05/19 13:45

一、一列数字规则如下:1、1、2、3、5、8、13、21、34..........求第30位数是多少,用递归算法实现

public class demo {

    /**
     * 实现方法
     * @param i
     * @return
     */
    public static int Foo(int i) {
        if (i <= 0) {
            return 0;
        } else if (i > 0 && i <= 2) {
            return 1;
        } else {
            return Foo(i - 1) + Foo(i - 2);
        }
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(demo.Foo(30));
    }
}

 

二、编程实现冒泡程序的算法

①、最经典的冒泡排序的方式

for (int j = 0; j < list.Length; j++)    {    for (i = list.Length - 1; i > j; i--)    {    if (list[j] < list[i])    {    temp = list[j];    list[j] = list[i];    list[i] = temp;    }    }


 ②、自带的排序方式

 static void Main(string[] args)  {  int[] a = { 3, 4, 7, 10, 5, 9 };  Array.Sort(a);  for (int i = 0; i < b.Length; i++)  {  Console.Write(b[i].ToString() + " ");  }  Console.ReadLine();  }  

 

③、

 int[] a = { 3, 4, 7, 10, 5, 9 };            var t = from x in a orderby x select x;            foreach (int i in t)                Console.WriteLine(i);


 

原创粉丝点击