1.编写程序,建立并输出一个10*10的矩阵,该矩阵对角线元素为1,其余元素均为0。输出该数组。
代码如下:
<span style="font-family:SimSun;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 矩?阵ó1
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[10, 10];
int i, j;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
{
if (i == j || i + j == 9)
a[i, j] = 1;
else
a[i, j] = 0;
}
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
Console.Write(a[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}</span>运行结果:
2.利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[20,50]内的整数,求它的两条对角线上元素之和。并统计小于35的个数,大于等于35的个数。
代码如下:
<span style="font-family:SimSun;font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 矩?阵ó
{
class Program
{
static void Main(string[] args)
{
int i,j;
int count_m = 0;
int count_n = 0;
int sum = 0;
int [,] arrary = new int[10,10];
Random run = new Random();
for(i=0;i<10;i++)
{
for (j = 0; j < 10; j++)
{
arrary[i,j] = run.Next(20,50);
if (j % 10 == 0)
{
Console.WriteLine();
}
Console.Write(arrary[i, j] + "\t" );
if (i == j || i + j == 9)
{ sum += arrary[i, j]; }
int mim = 35;
if (arrary[i, j] >= mim)
{ count_m++; }
if (arrary[i, j] < mim)
{ count_n++; }
}
}
Console.WriteLine();
Console.WriteLine("对角线之和:" + sum);
Console.WriteLine("大于等台于35的数的个数如下:" + count_m);
Console.WriteLine("小于35的数的个数如下:" + count_n);
Console.ReadLine();
}
}
}</span>运行结果:
269



被折叠的 条评论
为什么被折叠?



