作业
30人投票,从5个候选人选一个出来。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Class1 9 { 10 static void Main(string[] args) 11 { 12 //30人投票,从5个候选人选一个出来。 13 int[] vote = new int[5]; 14 for(int i=0;i<30;i++) 15 { 16 Console.Write("请第"+(i+1)+"位同学投票(0-4):"); 17 int temp = Convert.ToInt32(Console.ReadLine()); 18 if(temp <0 || temp >4) 19 { 20 Console.WriteLine("废票"); 21 continue; 22 } 23 else 24 { 25 vote[temp]++; 26 } 27 } 28 29 //计算最终得票。 30 int max = 0, maxSub = 0; 31 for(int i=0;i<vote.Length;i++) 32 { 33 //把每位候选人的票数显示出来。 34 Console.WriteLine("第" + (i + 1) + "号候选人的票数是:" + vote[i]); 35 //计算最大值。 36 if(vote[i] > max) 37 { 38 max = vote[i]; 39 maxSub = i; 40 } 41 } 42 43 //显示最终结果。 44 Console.WriteLine("最终投票结果为:"+(maxSub+1)+"号候选人当选,当选票数是"+max+"票。"); 45 } 46 } 47 }
冒泡排序
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Class2 9 { 10 static void Main(string[] args) 11 { 12 int[] a = new int[8] { 9, 12, 7, 5, 15, 2, 1, 8 }; 13 //冒泡排序。 14 for(int i=1;i<=a.Length-1;i++) //趟数 15 { 16 for (int j = 1; j <= a.Length - i; j++)//次数 17 { 18 if(a[j-1] > a[j]) 19 { 20 int t = a[j - 1]; 21 a[j - 1] = a[j]; 22 a[j] = t; 23 } 24 } 25 } 26 27 //显示 28 for(int k=0;k<a.Length;k++) 29 { 30 Console.WriteLine(a[k]); 31 } 32 } 33 } 34 }
折半查找
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Class3 9 { 10 static void Main(string[] args) 11 { 12 int[] a = new int[] { 3, 5, 7, 9, 11, 13, 14, 18 }; 13 14 Console.Write("请输入要找的数:"); 15 int find = Convert.ToInt32(Console.ReadLine()); 16 17 int top, bottom, mid; //上限下标,下限下标,中间下标 18 top = 0; 19 bottom = a.Length - 1; 20 21 while(bottom>=top) //只要下限下标还在上限下标的下面,就循环,否则没找到就结束。 22 { 23 //算中间下标 24 mid = (top + bottom) / 2; 25 //取中间的值 26 int n = a[mid]; 27 if(n < find) 28 { 29 top = mid + 1; //调整上限的下标 30 } 31 else if(n>find) 32 { 33 bottom = mid - 1;// 调整下限的下标。 34 } 35 else 36 { 37 Console.WriteLine("找到了,在第" + mid + "个元素上"); 38 break; 39 } 40 } 41 } 42 } 43 }
二维数组的应用
1.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Class2 9 { 10 static void Main(string[] args) 11 { 12 int[,] a = new int[3, 4]; 13 //输入 14 for (int i = 0; i < 3;i++ ) 15 { 16 a[i, 0] = i + 1; 17 Console.Write("语文:"); 18 a[i, 1] = Convert.ToInt32(Console.ReadLine()); 19 Console.Write("数学:"); 20 a[i, 2] = Convert.ToInt32(Console.ReadLine()); 21 a[i, 3] = a[i, 1] + a[i, 2]; 22 } 23 //输出 24 Console.WriteLine("学号\t语文\t数学\t总分\t"); 25 for (int n = 0; n < 3;n++ ) 26 { 27 for (int m = 0; m < 4;m++ ) 28 { 29 Console.Write(a[n,m]+"\t"); 30 } 31 Console.WriteLine(); 32 } 33 } 34 } 35 }
2.推箱子地图
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Class3 9 { 10 static void Main(string[] args) 11 { 12 int[,] map = new int[10, 10] { 13 {1,1,1,1,1,1,1,1,1,1}, 14 {1,0,0,0,0,1,0,0,0,1}, 15 {1,5,0,0,0,1,0,0,0,1}, 16 {1,0,0,0,0,1,1,0,0,1}, 17 {1,0,0,0,0,0,0,0,0,1}, 18 {1,0,0,0,0,0,0,0,0,1}, 19 {1,1,1,1,0,0,0,0,0,1}, 20 {1,0,0,0,0,0,0,0,0,1}, 21 {1,0,0,0,0,0,0,0,3,1}, 22 {1,1,1,1,1,1,1,1,1,1}, 23 }; 24 25 for (int i = 0; i < 10;i++ ) 26 { 27 for (int j = 0; j < 10;j++ ) 28 { 29 if (map[i, j] == 1) 30 { 31 Console.Write("■"); 32 } 33 if (map[i,j]==0) 34 { 35 Console.Write(" "); 36 } 37 if (map[i,j]==3) 38 { 39 Console.Write("※"); 40 } 41 if (map[i, j] == 5) 42 { 43 Console.Write("☆"); 44 } 45 } 46 Console.WriteLine(); 47 } 48 49 } 50 } 51 }