题目不难,总结如下:
1.冒泡排序:
冒泡排序
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 string temp=Console.ReadLine(); 6 string[] tempArray=temp.Split(' '); 7 int[] arry = new int[tempArray.Length]; 8 9 for (int i = 0; i < tempArray.Length; i++)10 {11 arry[i] = Convert.ToInt32(tempArray[i]);12 13 }14 15 PopSort(arry, true);16 PrintArray(arry);17 PopSort(arry,false);18 PrintArray(arry);19 }20 21 static void PrintArray(int[] arry)22 {23 for (int i = 0; i < arry.Length ; i++)24 {25 Console.Write("{0} ",arry[i]);26 }27 Console.WriteLine();28 }29 30 static void PopSort(int[] arry,bool Increase)31 {32 for(int i=0;iarry[j + 1])39 {40 int temp = arry[j];41 arry[j] = arry[j + 1];42 arry[j + 1] = temp;43 }44 }45 else46 {47 if (arry[j] < arry[j + 1])48 {49 int temp = arry[j];50 arry[j] = arry[j + 1];51 arry[j + 1] = temp;52 }53 }54 }55 }56 }57 }
2.字符串倒置
反序输出字符串
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 string temp = Console.ReadLine(); 6 Console.WriteLine("before convert:{0}", temp); 7 StringBuilder sb = new StringBuilder(temp); 8 for (int i = 0; i < sb.Length / 2; i++) 9 {10 char c = sb[i];11 sb[i] = sb[sb.Length - i - 1];12 sb[sb.Length - i - 1] = c;13 14 }15 16 temp = sb.ToString();17 Console.WriteLine("after convert:{0}",temp);18 19 20 }21 }
3.二分法查找数组中的某个值
二分查找
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int[] arry = new int[] { 23,45,67,78,100}; 6 int a=100; 7 int b = 23; 8 int c = 67; 9 int d = 40;10 Console.WriteLine("index of {0} is {1}",a, Find(arry, a));11 Console.WriteLine("index of {0} is {1}",b, Find(arry, b));12 Console.WriteLine("index of {0} is {1}",c, Find(arry, c));13 Console.WriteLine("index of {0} is {1}",d, Find(arry, d));14 }15 16 static int Find(int[] arry, int value)17 {18 int low = 0;19 int high = arry.Length;20 21 22 while (low <= high)23 {24 int mid = (low + high) / 2;25 if (arry[mid] == value)26 {27 return mid;28 }29 else if (arry[mid] < value)30 {31 low = mid + 1;32 }33 else34 {35 high = mid - 1;36 }37 38 }39 40 return -1;41 }42 }
4.输入数字m,n,从1,2,3...n数列中取几个数,使其和等于m,输出所有可能的组合。
View Code
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int m = 15; 6 int n = 9; 7 int[] arry = new int[n]; 8 BagProblem(m, n, arry); 9 }10 11 12 static void printArray(int[] arry)13 {14 for(int i=0;i