C#實現找二維數組中的鞍點


鞍點定義:該位置上的元素值在行中最大,在該列上最小

代碼示例:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NET_test1_5
{
    class Program
    {
        static void Main(string[] args)
        {
            int z=0;
            Console.WriteLine("請輸入數組的行數和列數");           
            int row = int.Parse(Console.ReadLine());
            int column = int.Parse(Console.ReadLine());
            int[,] a=new int[row,column];          
            Console.WriteLine("請輸入二維數組的值:");
            //輸入二維數組
           for (int x = 0; x < row; x++)
            {
                for (int y = 0; y < column; y++)
                {
                    a[x, y] = int.Parse(Console.ReadLine());
                }
            }
           //輸出二維數組
           Console.WriteLine("二維數組為:");
           for (int x = 0; x < row; x++)
           {
               Console.WriteLine();
               for (int y = 0; y < column; y++)
               {
                   Console.Write(a[x, y]+"    ");
               }
           }
           //找鞍點
           Console.WriteLine(); //換行
           for(int x=0;x<row;x++)
           {
             int max = a[x,0];  
             for(int y=0;y<column;y++)
             {
                 if(max < a[x,y])
                    max = a[x,y];
                 z = y;   
             }
             int min = a[0,z];   
             for(int y=0;y<row;y++)    
             {
                 if(min > a[x,z])  
                min = a[x,z];
                 }
                 if(min == max )
                 {
                     Console.WriteLine("該二維數組的鞍點為:"+max);
                 }
              }
           Console.ReadKey();
        }
    }
}

 

運行截圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM