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();
        }
    }
}
复制代码

运行结果:

posted @   睡觉不困  阅读(220)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示