马鞍点程序 马鞍点程序

时间:2011-07-11 10:34:17
【文件属性】:

文件名称:马鞍点程序 马鞍点程序

文件大小:916B

文件格式:SLN

更新时间:2011-07-11 10:34:17

马鞍点

using System;
using System.Collections.Generic;
using System.Text;

namespace 马鞍点
{

class Program
{
private static int m, n;
int[,] a;

public void getLine()
{
Console.WriteLine("输入你要测试的行:");
m = Convert.ToInt32(Console.ReadLine());
while (m <= 0)
{
Console.WriteLine("输入错误,请重新输入测试的行:");
Console.WriteLine("输入你要测试的行:");
m = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("输入你要测试的列:");
n = Convert.ToInt32(Console.ReadLine());
while (n <= 0)
{
Console.WriteLine("输入错误,请重新输入测试的列:");
Console.WriteLine("输入你要测试的列:");
m = Convert.ToInt32(Console.ReadLine());
}
a = new int[m, n];
}

public void RunRandom(Random randObj)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i, j] = (randObj.Next())%100;
}
}
}

public void Random(int seed)
{
Random fixRand = new Random(seed);
RunRandom(fixRand);
}

public void getArray()
{
Console.WriteLine("输入的值为:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write("a[{0},{1}] = ", i,j);
a[i,j] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine();
}
}

public void displayArray()
{
Console.WriteLine("你输入的矩阵为:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(a[i,j]);
Console.Write("\t");
}
Console.WriteLine();
}
}

public void compare()
{
int flag = 0;
Console.WriteLine("马鞍点为:");
for (int i = 0; i < m; i++)
{
int k ,min,max,t;
for (int j = 0; j < n; j++)
{
min = a[i,j];
for (k = 0; k < n; k++)
{
if (min > a[i, k])
{
break;
}
}
if (k == n)
{
max = a[i, j];
for (t = 0; t < m; t++)
{
if (max < a[t, j])
{
break;
}
}
if (t == m)
{
Console.WriteLine(a[i, j]);
Console.WriteLine("\t");
flag = 1;
}
}
}
}
if (flag == 0)
{
Console.WriteLine("此矩阵没有马鞍点!");
}
}

public static void Main(string[] args)
{
Program p = new Program();
bool Flag = true;
while (Flag)
{
Console.WriteLine(" | 马鞍点程序 |");
Console.WriteLine(" |=============================================|");
Console.WriteLine(" | 0--------退出 |");
Console.WriteLine(" | 1--------随机输入 |");
Console.WriteLine(" | 2--------手动输入 |");
Console.WriteLine(" |=============================================|");
Console.Write("你输入的是:");
int num = int.Parse(Console.ReadLine());
switch (num)
{
case 0:
Flag = false;
break;
case 1:
p.getLine();
p.Random(1);
p.displayArray();
p.compare();
break;
case 2:
p.getLine();
p.getArray();
p.displayArray();
p.compare();
break;
default:
Console.WriteLine("输入错误,请重新输入!");
break;
}
Console.ReadLine();
}
}
}
}


网友评论