C#编写程序,找一找一个二维数组中的鞍点
编写程序,找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该列上最小。有可能数组没有鞍点)。要求:
1.二维数组的大小、数组元素的值在运行时输入;
2.程序有友好的提示信息。
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lab05 { class Program { static void Main(string[] args) { int row,column; Console.WriteLine("请输入二维数组的行数:"); row=int.Parse(Console.ReadLine()); Console.WriteLine("请输入二维数组的列数:"); column=int.Parse(Console.ReadLine()); int[,] a=new int[row,column]; for (int i = 0; i < row; i++) { Console.WriteLine("请输入第{0}行:",i+1); for (int j = 0; j < column; j++) { a[i,j] = int.Parse(Console.ReadLine()); } } // Console.WriteLine("这是第1行2Lie:{0}", a[0,1]); int max, maxj; bool flag=true; for (int i = 0; i < row; i++) { max = a[i, 0]; maxj = 0; for (int j = 0; j < column; j++) { if (max < a[i, j]) { max = a[i, j]; maxj = j; } } flag = true; for(int k=0;k<row;k++){ if (max > a[k, maxj]) { flag = false; continue; } } if (flag) { Console.Write("鞍点({0},{1})",i+1,maxj+1); break; } } if (!flag) { Console.Write("不存在鞍点!"); } Console.Write("\n请按任意键继续..."); Console.ReadKey(); } } }
运行结果:


浙公网安备 33010602011771号