幾個常用經典演算法總結

iDotNetSpace發表於2010-05-21

1。從鍵盤輸入100個數字,編寫程式碼對它們排序

2.一組數值排列規則為:1,1,2,3,5,8,13,21,34……,請用遞迴演算法求第30位數值

3. 使用者輸入20個數字,如何快速選出最大的數字和最小的數字

4.假設藍光DVD光碟8元/張,普通DVD光碟為3元/張,CD光碟為0.25元/張。

  某客戶希望花100元購買這3種光碟,購買數量為100張。求各為多少張?

5.如何在從小到大排序的數字序列中搜尋使用者指定的數字,並返回這個數字在序列中的位置?

6.隨即輸出1到100內的整數

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Suanfa

{

   class Program

    {

 

       static void Main(string[] args)

       {

           //Paixun.Paixu();

           Suiji.Sui();

           //Suiji.Sui2();

           //Pannumber.PanShu();

           //Maxmin.Maxm();

           //Digui.GetNum();

           //Sousuo.Sous();

 

       }

    }

          //1.從鍵盤輸入100個數字,編寫程式碼對它們排序

          class Paixun

    {

      public static void Paixu()

       {

           int i, j, temp;

           int[] MyArr = new int[10];//定義int型別MyArr陣列,包含10個子項

           for (i = 0; i < 10; i++)

           {

                MyArr[i] =Convert.ToInt32(Console.ReadLine());

 

 

           }

           for (j = MyArr.Length - 1; j > 0; j--)

           {

                for (i = 0; i < j; i++)

                {

                    //如果當前子項值大於其後的子項值,則將子項值交換

                    if (MyArr[i] > MyArr[i +1])

                    {

                        temp = MyArr[i];

                        MyArr[i] = MyArr[i +1];

                        MyArr[i + 1] = temp;

 

                    }

                }

           }

           //輸出MyArr陣列中所有的諮詢值

           for (i = 0; i < MyArr.Length; i++)

           {

               Console.WriteLine(MyArr[i].ToString());

           }

           Console.ReadKey();

       }

    }

          //2.一組數值排列規則為:1,1,2,3,5,8,13,21,34……,請用遞迴演算法求第30位數值

         classDigui

    {

 

       public static int  Dig( int n)

       {

           if (n <= 1)

           {

                return 1;

           }

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

 

       }

       public static void GetNum()

       {

           Console.WriteLine("第30個的值為:{0}",Dig(29));

           Console.ReadKey();

        }

    }

          //3.使用者輸入20個數字,如何快速選出最大的數字和最小的數字

         classMaxmin

    {

 

       public static void Maxm()

       {

           int i, temp, max, min;

           int[] MyArr = new int[20];

           for (i = 0; i < 20; i++)

           {

                MyArr[i] =Convert.ToInt32(Console.ReadLine());

           }

           //將陣列子項中值較小的部分放置於陣列的前半部分

           //將陣列子項中值較大的部分放置於陣列的後半部分

           for (i = 0; i < (MyArr.Length + 1) / 2; i++)

           {

                if (MyArr[i] > MyArr[MyArr.Length- 1 - i])

                {

                    temp = MyArr[i];

                    MyArr[i] =MyArr[MyArr.Length - 1 - i];

                    MyArr[MyArr.Length - 1 - i]= temp;

                }

           }

           //將陣列索引號為0的子項值付給min變數

           min = MyArr[0];

           //將末端的子項賦給max變數

           max = MyArr[MyArr.Length - 1];

           //前半部分比較

           for (i = 0; i < (MyArr.Length + 1) / 2; i++)

           {

                if (min > MyArr[i])

                {

                    min = MyArr[i];

                }

           }

           for (i = (MyArr.Length / 2); i < MyArr.Length; i++)

           {

                if (max < MyArr[i])

                {

                    max = MyArr[i];

                }

            }

           Console.WriteLine("最小值為:{0},最大值為:{1}", min, max);

           Console.ReadKey();

 

 

       }

    }

         //4.假設藍光DVD光碟8元/張,普通DVD光碟為3元/張,CD光碟為0.25元/張。某客戶希望花100元購買這3種光碟,購買數量為100張。求各為多少張?

          class Pannumber

    {

       //假設藍光DVD光碟8元/張,普通DVD光碟為3元/張,CD光碟為0.25元/張。

       //某客戶希望花100元購買這3種光碟,購買數量為100張。求各為多少張?

       public static void PanShu()

       {

           //i,j,k代表每種光碟是購買數量,m代表購買總數,n代表總價格

           double i, j, k, m, n;

           for (i = 0; i < 100; i++)

           {

                for (j = 0; j < 100; j++)

                {

                    for (k = 0; k < 100;k++)

                    {

                        m = i + j + k;

                        n = 8 * i + 3 * j +0.25 * k;

                        if ((m == 100)&& (n == 100))

                        {

                           Console.WriteLine("{0},{1},{2}",i,j,k);

                            Console.ReadKey();

                        }

                    }

                }

           }

 

 

       }

    }

          //5.如何在從小到大排序的數字序列中搜尋使用者指定的數字,並返回這個數字在序列中的位置?

           class Sousuo

    {

 

       public static void Sous()

       {

           int[] MyArr={100,102,105,108,109,112};

           Console.WriteLine("輸入所需查詢的分數:");

           int score = Convert.ToInt32(Console.ReadLine());

           int index = SelectScore(MyArr, score, MyArr.Length);

           if (index < 0)

           {

                Console.WriteLine("你查詢的分數不在MyArr成績陣列中");

           }

           else

           {

                Console.WriteLine("你找查的分數已在MyArr成績陣列中找到,其所在子項索引號為:{0}", index.ToString());

           }

           Console.ReadKey();

       }

       public static int SelectScore(int[] arr, int score, int n)

       {

           int left = 0;

           int right = n - 1;

           int middle;

           while (left <= right)

           {

                middle = (left + right) / 2;

                if (score == arr[middle])

                {

                    return middle;

                }

                if (score < arr[middle])

                {

                    right = middle - 1;

                }

                else

                {

                    left = middle - 1;

                }

           }

           return -1;

       }

    }

         //6.隨即輸出1到100內的整數

            class Suiji

    {

                   //方法1

       public static void Sui()

       {

           int[] intArr = new int[100];

           ArrayList myList = new ArrayList();

           Random rnd = new Random();

           while (myList.Count <= 100)

           {

                int num = rnd.Next(1, 102);

                if (!myList.Contains(num))

                    myList.Add(num);

           }

           //myList.Sort();

           for (int i = 0; i < 100; i++)

           {

                intArr[i] = (int)myList[i];

                Console.WriteLine(intArr[i]);

           }

 

           Console.ReadKey();

 

       }

                   //方法2

       public static void Sui2()

       {

           //可以跑下這段程式碼,C#點的:

           int len = 100;

           List array = new List();

           for (int i = 0; i < len; i++)

           {

                array.Add(i + 1);

           }

 

           //如果希望打亂順序加上下面這段(Shuffle,洗牌演算法)

           Random r = new Random();

           for (int i = 0; i < len - 1; i++)

           {

                int tmp = array[i];

                int pos = i + r.Next(0, len -i);

                array[i] = array[pos];

                array[pos] = tmp;

           }

 

           //順序排序

           //array.Sort();

 

           //希望控制檯輸出:

           foreach (int a in array)

           {

                Console.WriteLine(a);

           }

 

           Console.ReadKey();

       }

    }

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-663384/,如需轉載,請註明出處,否則將追究法律責任。

相關文章