展开全部
using System;
class Program
{
static void Main(string[] args)
{
int[,] array = new int[5, 5];
int i, j, k;
int max, min, max_row, max_col, min_row, min_col, sum = 0;
string str1, str2 = "";
Console.WriteLine("请输入五行数据,每行五个数据,以空格隔开,每行输入完之后回车(输入格式不对会出错):");
for (i = 0; i < 5; i++)
{
str1 = Console.ReadLine();
j = 0;
k = 0;
while (j < str1.Length)
{
if (str1[j] != ' ')
str2 += str1[j];
else
{
array[i, k++] = int.Parse(str2);
str2 = "";
}
j++;
}
array[i, k] = int.Parse(str2);
str2 = "";
}
max = min = array[0,0];
max_row = max_col = 1;
min_row = min_col = 1;
for(i=0; i < 5; i++)
for (j = 0; j < 5; j++)
{
if (max < array[i, j])
{
max = array[i, j];
max_row = i + 1;
max_col = j + 1;
}
if (min > array[i, j])
{
min = array[i, j];
min_row = i + 1;
min_col = j + 1;
}
}
for (i = 0; i < 5; i++)
sum += array[i, i];
Console.WriteLine("最大数为:{0},位置是:第{1}行,第{2}列", max, max_row, max_col);
Console.WriteLine("最小数为:{0},位置是:第{1}行,第{2}列", min, min_row, min_col);
Console.WriteLine("对角线上的元素之和为:{0}", sum);
}
}
class Program
{
static void Main(string[] args)
{
int[,] array = new int[5, 5];
int i, j, k;
int max, min, max_row, max_col, min_row, min_col, sum = 0;
string str1, str2 = "";
Console.WriteLine("请输入五行数据,每行五个数据,以空格隔开,每行输入完之后回车(输入格式不对会出错):");
for (i = 0; i < 5; i++)
{
str1 = Console.ReadLine();
j = 0;
k = 0;
while (j < str1.Length)
{
if (str1[j] != ' ')
str2 += str1[j];
else
{
array[i, k++] = int.Parse(str2);
str2 = "";
}
j++;
}
array[i, k] = int.Parse(str2);
str2 = "";
}
max = min = array[0,0];
max_row = max_col = 1;
min_row = min_col = 1;
for(i=0; i < 5; i++)
for (j = 0; j < 5; j++)
{
if (max < array[i, j])
{
max = array[i, j];
max_row = i + 1;
max_col = j + 1;
}
if (min > array[i, j])
{
min = array[i, j];
min_row = i + 1;
min_col = j + 1;
}
}
for (i = 0; i < 5; i++)
sum += array[i, i];
Console.WriteLine("最大数为:{0},位置是:第{1}行,第{2}列", max, max_row, max_col);
Console.WriteLine("最小数为:{0},位置是:第{1}行,第{2}列", min, min_row, min_col);
Console.WriteLine("对角线上的元素之和为:{0}", sum);
}
}
更多追问追答
追问
首先非常感谢您的帮助!不过,这是用C#来编写的?
追答
是啊 如假包换
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询