急求几道C#题,请用最基本语言谢谢.
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/12 00:32:07
急求几道C#题,请用最基本语言谢谢.
1.求两个矩阵的乘积.假定一个矩阵A为 3 行 4 列,另一个矩阵B为 4 行 3列 ,根据矩阵乘法的规则,其乘积C为一个 3 行 3 列的矩阵.
2.打印杨辉三角形.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
3.使用二维数组,定义一个3X3的矩阵,求出对角线之和,并输出.
1.求两个矩阵的乘积.假定一个矩阵A为 3 行 4 列,另一个矩阵B为 4 行 3列 ,根据矩阵乘法的规则,其乘积C为一个 3 行 3 列的矩阵.
2.打印杨辉三角形.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
3.使用二维数组,定义一个3X3的矩阵,求出对角线之和,并输出.
没有人回答你吗?
我帮你 不过你最好还是好好看书 多学点没有坏处
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][]{new int[]{1,1,1,1},new int[] {1,1,1,1},new int[]{1,1,1,1} };
int[][] b = new int[][] { new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 } };
int[][] c = new int[3][] { new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 } };
for (int i = 0; i < 3; i++)
{
for (int ii = 0; ii < 3; ii++)
{
int answer = 0;
for (int j = 0; j < 4; j++)
{
answer += a[i][j] * b[j][ii];
}
c[i][ii] = answer;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(c[i][j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.Read();
}
}
}
第二题
杨辉三角 右边填充0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][] { new int[] { 1, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 } };
for (int i = 1; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
if (j == 0)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
if(a[i][j]!=0)
Console.Write(a[i][j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.Read();
}
}
}
第三题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][] { new int[] { 1, 0, 0 }, new int[] { 0, 1, 0 }, new int[] { 0, 0, 1 } };
int sum = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if(i==j)
sum += a[i][j];
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(a[i][j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.Write(" the sum is {0}", sum);
Console.Read();
}
}
}
我帮你 不过你最好还是好好看书 多学点没有坏处
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][]{new int[]{1,1,1,1},new int[] {1,1,1,1},new int[]{1,1,1,1} };
int[][] b = new int[][] { new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 } };
int[][] c = new int[3][] { new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 } };
for (int i = 0; i < 3; i++)
{
for (int ii = 0; ii < 3; ii++)
{
int answer = 0;
for (int j = 0; j < 4; j++)
{
answer += a[i][j] * b[j][ii];
}
c[i][ii] = answer;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(c[i][j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.Read();
}
}
}
第二题
杨辉三角 右边填充0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][] { new int[] { 1, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0 } };
for (int i = 1; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
if (j == 0)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
if(a[i][j]!=0)
Console.Write(a[i][j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.Read();
}
}
}
第三题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][] { new int[] { 1, 0, 0 }, new int[] { 0, 1, 0 }, new int[] { 0, 0, 1 } };
int sum = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if(i==j)
sum += a[i][j];
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(a[i][j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.Write(" the sum is {0}", sum);
Console.Read();
}
}
}