算法题总结

1.一列数据的规则如下:1、1、2、3、5、8、13、21、34......求第30位数是多少,用递归的方法来做:

    public static int fib(int n)//n是第n位数是多少
        {
            if (n == 1 || n == 2)
            {
                return 1;
            }
            else
            {
                return fib(n - 1) + fib(n - 2);
            }
        }

2.对于反序的操作reverse(int[] array, int begin, int end)方法:

  public static void reverse(int[] array,int begin, int end)
 {
        while (end > begin)
       {
                int temp = array[begin];
                array[begin] = array[end];
                array[end] = temp;
                begin++;
                end--;
        }     
 }
      

3.从一个数组中截取m个数,并让顺序是随机的,如何做

4.冒泡排序(从小到大排序)

   public static void maopao(int[] a)
    {
         int temp = 0;
         for (int i = 0; i < a.Length-1; i++)
         {
              for (int j = i+1; j < a.Length; j++)
              {
                  if (a[j] < a[i])
                  {
                      temp = a[j];
                      a[j]=a[i];
                      a[i]=temp ;
                  }
              }
         }
    }
5.1-10000中的9有多少?
 答:
int count=0;//9的总数量
for(int i=1;i<=10000;i++)
{
      int number=i;
      do
      {
             if(number%10==9)
             count++;
             number=number/10;
      }while(number!=0)     
}    
。net工程师
原文地址:https://www.cnblogs.com/yuners/p/10987223.html