这才是今天的实训,没想到吧,哈哈哈!
第1关:逆序输出数组元素

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch701 { class Program { static void Main(string[] args) { /******begin*******/ int n = Convert.ToInt32(Console.ReadLine()); if (n < 0 || n > 10) { Console.WriteLine("input error!"); return ; } int[] a = new int[n]; for (int i = 0; i < n; ++i) { a[i] = Convert.ToInt32(Console.ReadLine()); } Array.Reverse(a); for (int i = 0; i < n; ++i) { Console.Write("{0} ", a[i]); } /*******end********/ } } }
第2关:数据排序

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch702 { class Program { static void Main(string[] args) { /******begin*******/ int n = Convert.ToInt32(Console.ReadLine()); if (n < 0 || n > 10) { Console.WriteLine("input error!"); return ; } int[] a = new int[n]; for (int i = 0; i < n; ++i) { a[i] = Convert.ToInt32(Console.ReadLine()); } Array.Sort(a); for (int i = n - 1; i >= 0; -- i) { Console.Write("{0} ", a[i]); } /*******end********/ } } }
第3关:求数组中的最大值和最小值

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch705 { class Program { static void Main(string[] args) { /******begin*******/ try { int n = Convert.ToInt32(Console.ReadLine()); if (n < 0 || n > 10) { Console.WriteLine("input error!"); return; } int[] a = new int[n]; for (int i = 0; i < n; ++i) { a[i] = Convert.ToInt32(Console.ReadLine()); } int max = -1, min = 1000000; for (int i = 0; i < n; ++i) { if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; } Console.WriteLine("最大值:{0}", max); Console.WriteLine("最小值:{0}", min); } catch (Exception e) { Console.WriteLine("input error!"); } /*******end********/ } } }
第4关:十进制数转换为二进制数

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch704 { class Program { static void Main(string[] args) { /******begin*******/ int n = Convert.ToInt32(Console.ReadLine()); if (n <= 0 || n > 2147483647) { Console.WriteLine("input error!"); return; } Stack<int> s = new Stack<int>(); while (n != 0) { s.Push(n % 2); n /= 2; } while (s.Count != 0) { Console.Write(s.Peek()); s.Pop(); } Console.WriteLine(); /*******end********/ } } }
第5关:二维数组行列互换

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch705 { class Program { static void Main(string[] args) { /******begin*******/ int[,] a = new int[4, 4]; int[,] b = new int[4, 4]; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { int.TryParse(Console.ReadLine(), out a[i, j]); } } Console.WriteLine("初始状况:"); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { Console.Write("{0} ", a[i, j]); } Console.WriteLine(); } for (int j = 0; j < 4; ++j) { for (int i = 0; i < 4; ++i) { b[j, i] = a[i, j]; } } Console.WriteLine("互换后:"); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { Console.Write("{0} ", b[i, j]); } Console.WriteLine(); } /*******end********/ } } }
第6关:输出杨辉三角

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ch706 { class Program { static void Main(string[] args) { /******begin*******/ int n = Convert.ToInt32(Console.ReadLine()); int[,] a = new int[n, n]; for (int i = 0; i < a.GetLength(0); ++i) { for (int j = 0; j <= i; ++j) { if(j == 0 || i == j) { a[i, j] = 1; } else { a[i,j] = a[i - 1,j] + a[i-1,j-1]; } Console.Write(a[i,j].ToString() + " "); } Console.WriteLine(); } /*******end********/ } } }
呼~~~终于搞完了,嘻嘻!