• C#编写程序找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该 列上最小。有可能数组没有鞍点)。要求:1.二维数组的大小、数组元素的值在运行时输入;2.程序有友好的提示信息。


     1 using System;
     2 using System.Collections.Generic;
     3 
     4 namespace Test_1_5
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             int max, min, rowindex=0,colindex=0, flag = 0;
    11 
    12             Console.WriteLine("选择数组的行数:");
    13             string input1 = Console.ReadLine();
    14             int data1 = Convert.ToInt32(input1);
    15 
    16             Console.WriteLine("选择数组的列数:");
    17             string input2 = Console.ReadLine();
    18             int data2 = Convert.ToInt32(input2);
    19 
    20         xunhuan:
    21             Console.WriteLine("请输入{0}行{1}列的数组,空格分隔",data1, data2);
    22             int[,] a = new int[data1, data2];
    23                 for (int i = 0; i < data1; i++)
    24                 {
    25                     string str = Console.ReadLine();//首先输入一字符串,表示二维数组的一行数据
    26                     string[] tmp = str.Split("".ToCharArray());//通过Split方法以空格作为分隔符将输入的一行字符串分开
    27                     for (int j = 0; j < data2; j++)
    28                     {
    29                         if (tmp.Length != data2)
    30                         {
    31                         Console.WriteLine("输入格式有误");
    32                         goto xunhuan;
    33                         }
    34                         else
    35                         {
    36                         a[i, j] = int.Parse(tmp[j]);//将分割后的字符赋值给二维数组的每个元素
    37                         }
    38                     }
    39                 }
    40 
    41             Console.WriteLine("二维数组为:");
    42             for(int i = 0; i < data1; i++)
    43             {
    44                 for(int j = 0; j < data2; j++)
    45                 {
    46                     Console.Write(a[i, j] + "  ");
    47                 }
    48                 Console.WriteLine();
    49             }
    50 
    51             for(int i= 0; i < data1; ++i)
    52             {
    53                 //找到i行上最大的元素,记录该元素所在的列号colindex
    54                 max = a[i, 0];
    55                 for(int j = 0; j < data2; ++j)
    56                 {
    57                     if (a[i, j] > max)
    58                     {
    59                         max = a[i, j];
    60                         colindex = j;
    61                     }
    62                 }
    63                 min = a[0, colindex];
    64                 //找max所在列上最小元素,记录所在行rowindex
    65                 for (int j = 0; j < data1; ++j)
    66                 {
    67                     if (a[j, colindex] < min)
    68                     {
    69                         min = a[j, colindex];
    70                         rowindex = j;
    71                     }
    72                 }
    73                 if (max == min && i == rowindex)
    74                 {
    75                     flag = 1;
    76                     Console.WriteLine("鞍点为{0}行{1}列的元素{2}", rowindex+1, colindex+1, max);
    77                     break;
    78 
    79                 }
    80             }
    81             if (0 == flag)
    82             {
    83                 Console.WriteLine("没有鞍点");
    84             }
    85 
    86         }
    87     }
    88 }

    截图

     

  • 相关阅读:
    获取计算机名称
    imagelist用法
    cxgrid的ImageComboBox属性学习
    MlskincolorButton使用方法
    delphi实现窗体组建随窗体大小改变而改变
    判断路径下文件是否存在
    Delphi 按Esc快捷键退出程序的简单方法
    pagecontrol
    LinkedList源码解析
    ArrayList源码分析
  • 原文地址:https://www.cnblogs.com/dty602511/p/15599300.html
Copyright © 2020-2023  润新知