用C#,利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[20,50]内的整数,输出其最大值和下标.

问题描述:

用C#,利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[20,50]内的整数,输出其最大值和下标.
1个回答 分类:综合 2014-12-09

问题解答:

我来补答
C#源程序:class Program{    static void Main()    {        Random rand = new Random();        int[,] arr = new int[4, 4];     //二维矩阵(4×4)        int row, col;        int num;        int max;                        //最大值        int x, y;                       //最大值的下标(x, y)        for (row = 0; row < 4; row++)        {            for (col = 0; col < 4; col++)            {                while (true)                {                    num = rand.Next(51);                    if (num >= 20 && num <= 50)                    {                        break;                    }                }                arr[row, col] = num;            }        }        x = 0;        y = 0;        max = arr[0, 0];        for (row = 0; row < 4; row++)        {            for (col = 0; col < 4; col++)            {                if (arr[row, col] > max)                {                    max = arr[row, col];                    x = row;                    y = col;                }            }        }        //输出二维矩阵        System.Console.WriteLine("二维矩阵:");        for (row = 0; row < 4; row++)        {            for (col = 0; col < 4; col++)            {                System.Console.Write("{0,4}", arr[row, col]);            }            System.Console.WriteLine();        }        //输出最大值及下标        System.Console.WriteLine("最大值:{0},下标({1},{2})", max, x, y);    }}运行测试:
 
 
展开全文阅读
剩余:2000
下一页:原创8