简单排序方法

来源:互联网 发布:企业管理优化建议 编辑:程序博客网 时间:2024/06/04 17:59

1.冒泡法

void  Sort(int[] array){for(int i= 0; i< array.Length; i++){for(int j = 0; j<array.Length - i-1; j++){if(array[j]>array[j+1]){int b = array [j + 1];array [j + 1] = array [j];array [j] = b;}}}for(int k = 0; k<array.Length;k++){UnityEngine.Debug.Log (array [k]);}}


2.选择法

void  Sort(int[] array){int Min = 0;for(int i= 0; i< array.Length; i++){Min = i;for(int j = i+1; j<array.Length; j++){if(array[j]< array[Min]){Min = j;}}int b = array [i];array [i] = array [Min];array [Min] = b;}for(int k = 0; k<array.Length;k++){UnityEngine.Debug.Log (array [k]);}}


3.插入法

void  Sort(int[] array){for(int i= 1; i< array.Length; i++){int b = array [i];for(int j = i-1; j >= 0; j--){if(array[j]> b){array [j+1] = array [j];}else{array [j+1] = b;break;}if(j -1 <0){array [j] = b;}}}for(int k = 0; k<array.Length;k++){UnityEngine.Debug.Log (array [k]);}}

2、有一数列1,1,2,3,5,.......求第30个数


public class MainClass{  public static void Main()  {    Console.WriteLine(Foo(30));  }  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);  }}

3、程序设计:猫大叫一声,所有的老鼠开始逃跑,主人被惊醒

  public delegate void SubEventHandler();   public abstract class Subject   {     public event SubEventHandler SubEvent;     protected void FireAway()     {       if (this.SubEvent != null)         this.SubEvent();     }    }   public class Cat : Subject   {     public void Cry()     {       Console.WriteLine(cat cryed.);       this.FireAway();     }   }   public abstract class Observer   {     public Observer(Subject sub)     {       sub.SubEvent += new SubEventHandler(Response);     }     public abstract void Response();    }   public class Mouse : Observer   {     private string name;     public Mouse(string name, Subject sub) : base(sub)     {        this.name = name;     }     public override void Response()     {       Console.WriteLine(name + attempt to escape!);     }   }   public class Master : Observer   {     public Master(Subject sub) : base(sub){}     public override void Response()     {       Console.WriteLine(host waken);     }   }   class Class1   {     static void Main(string[] args)     {       Cat cat = new Cat();       Mouse mouse1 = new Mouse(mouse1, cat);       Mouse mouse2 = new Mouse(mouse2, cat);       Master master = new Master(cat);       cat.Cry();     }   } 

4、有一个字符串“I am a good man”,设计一个函数,返回“man good a am I”。

static string Reverse() {  string s = "I am a good man";  string[] arr = s.Split(' ');  string res = "";  for (int i = arr.Length - 1; i >= 0; i--)  {    res += arr[i];    if (i > 0)   res += " ";  }  return res; }

5、A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些人参加了竞赛:

(1)A参加时,B也参加;

(2)B和C只有一个人参加;

(3)C和D或者都参加,或者都不参加;

(4)D和E中至少有一个人参加;

(5)如果E参加,那么A和D也都参加。

static void Main(string[] args){  char[] name={'A','B','C','D','E'};  int[] value = new int[5];  for (value[0]=0;value[0]<2;value [0]++) for (value[1]=0; value[1] < 2; value[1]++)   for (value[2]=0; value[2] < 2; value[2]++) for (value[3]=0; value[3] < 2; value[3]++)   for (value[4]=0; value[4] < 2; value[4]++)   {  if ((value[1] >= value[0]) && (value[1] + value[2] == 1) && (value[2] == value[3]) && (value[3] + value[4]==1) && (value[4]==0 || value[4]==1 && value[0]==1 && value[3]==1))  {    for (int i = 0; i < 5; i++)    {  if (value[i]==1)  {    Console.WriteLine("{0}参加", name[i]);  }  else  {    Console.WriteLine("{0}不参加", name[i]);  }    }  }   }}

6、a user entered an integer value into a text box.Without using a buit-in library, convert the numeric string to its integerrepresentation.

static int StringTolnt(string s){  int sum = 0;  for (int i = 0; i < s.Length; i++) sum = sum * 10 + (s[i] - '0');  return sum;}

0 0