稀疏矩阵运算器 (Java)加减乘```

aabisou 2009-06-22 09:06:04
加精
``我们数据结构课程设计的一道题...


【问题描述】
稀疏矩阵是指那些多数元素为零的矩阵。利用“稀疏”特点进行存储和计算可以大大节省存储空间,提高计算效率。实现一个能进行稀疏矩阵基本运算的运算器。
【基本要求】
以“带行逻辑链接信息”的三元组顺序表表示稀疏矩阵,实现两个矩阵相加、相减和相乘的运算。稀疏矩阵的输入形式采用三元组表示,而运算结果的矩阵则以通常的矩阵形式列出。
【实现提示】
(1)首先应输入矩阵的行数和列数,并判别给出的两个矩阵的行、列数对于所要求作的运算是否相匹配。可设矩阵的行数和列数均不超过20。
(2)程序可以对三元组的输入顺序加以限制,例如,按行优先。
(3)在用三元组表示稀疏矩阵时,相加或相减所得结果矩阵应该另生成,乘积矩阵也可用二维数组存放。

我的邮箱是aabisouya@163.com


没一点头绪....帮忙做下...加 减 乘中的任意一种..

剩下的自己来做``

拜托啦/...分数不多...给100吧..
...全文
1114 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
czq563522753 2010-03-31
  • 打赏
  • 举报
回复
学习了学习了学习了学习了学习了
fibbery 2010-02-02
  • 打赏
  • 举报
回复
对上面的程序重新写了一遍,简化判断的。再补上一个稀疏矩阵乘积的算法。
// 计算两个三元组表示的矩阵之和,返回值是稀疏矩阵的三元组表示。
private static TripleSMatrix addTripleMatrix(TripleSMatrix t1,
TripleSMatrix t2) {
if (t1.getRows() != t2.getRows() || t1.getColumns() != t2.getColumns()) {
System.out.println("这两个矩阵不能相加 。");
return null;
}
TripleSMatrix sum = new TripleSMatrix();

int count = 1;
int i = 1, j = 1, v;
sum.setRows(t1.getRows());
sum.setColumns(t1.getColumns());

// 执行矩阵相加,按行的顺序相加,分为四种情况
while (i <= t1.getNonzeroElements() && j <= t2.getNonzeroElements()) {
if ((t1.triple[i][0] < t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] < t2.triple[j][1])) {

sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
} else if ((t1.triple[i][0] > t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] > t2.triple[j][1])) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
}
// 当前结点对应且两元素之和不为零
else if ((t1.triple[i][2] + t2.triple[j][2]) != 0) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2] + t2.triple[j][2];
count++;
i++;
j++;
} else {// 当前结点对应且两元素之和为零,此种情况在三元组中将不再存储
i++;
j++;
}
}
while (i <= t1.getNonzeroElements()) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
}

while (j <= t2.getNonzeroElements()) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
}

sum.setNonzeroElements(count - 1);
sum.sortTriple(sum.triple, 1, count);// 对输入的三元组排序
return sum;
}

// 计算两个求稀疏矩阵的和 ,结果用数组表示。和为零的值不用单独做判断了这次
private static int[][] addMatrix(TripleSMatrix t1, TripleSMatrix t2) {
if (t1.getRows() != t2.getRows() || t1.getColumns() != t2.getColumns()) {
System.out.println("这两个矩阵不能相加 。");
return null;
}
int[][] c = new int[t1.getRows()][t2.getColumns()];
int i = 1, j = 1;
// 执行矩阵相加,按行的顺序相加,分为四种情况
while (i <= t1.getNonzeroElements() && j <= t2.getNonzeroElements()) {
if ((t1.triple[i][0] < t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] < t2.triple[j][1])) {

c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
} else if ((t1.triple[i][0] > t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] > t2.triple[j][1])) {

c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t2.triple[j][2];
j++;
} else {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2]
+ t2.triple[j][2];
i++;
j++;
}
}

// 将矩阵t1 的剩余元素插入矩阵c
while (i <= t1.getNonzeroElements()) {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
}
// 将矩阵t2 的剩余元素插入矩阵c
while (j <= t2.getNonzeroElements()) {
c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t2.triple[i][2];
j++;
}
return c;
}
}
fibbery 2010-02-02
  • 打赏
  • 举报
回复
矩阵加法 那个算法有问题,另外这里返回值不是稀疏矩阵了,又变成了一个耗内存的大数组。我自己加了返回值是三元组的 稀疏矩阵的加法算法。
bigbug9002 的加法函数 条件没有考虑全面。我尝试着写矩阵乘法的算法,感觉结果总是不对,不知道错在哪里了?

// 计算两个三元组表示的矩阵之和,返回值是稀疏矩阵的三元组表示。
private static TripleSMatrix addTripleMatrix(TripleSMatrix t1,
TripleSMatrix t2) {
if (t1.getRows() != t2.getRows() || t1.getColumns() != t2.getColumns()) {
System.out.println("这两个矩阵不能相加 。");
return null;
}
TripleSMatrix sum = new TripleSMatrix();

int count = 1;
int i = 1, j = 1, v;
sum.setRows(t1.getRows());
sum.setColumns(t1.getColumns());

while (i <= t1.getNonzeroElements() && j <= t2.getNonzeroElements()) {
if (t1.triple[i][0] < t2.triple[j][0]
&& i <= t1.getNonzeroElements()) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
} else if (t1.triple[i][0] > t2.triple[j][0]
&& j < t2.getNonzeroElements()) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
} else {
if (t1.triple[i][1] < t2.triple[j][1]
&& i <= t1.getNonzeroElements()) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
} else if (t1.triple[i][1] > t2.triple[j][1]
&& j <= t2.getNonzeroElements()) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
} else {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2] + t2.triple[j][2];

count++;

i++;
j++;
}
}
}

if (i > t1.getNonzeroElements()) {
while (j <= t2.getNonzeroElements()) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
}
} else if (j > t2.getNonzeroElements()) {
while (i <= t1.getNonzeroElements()) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
}
}

sum.setNonzeroElements(count - 1);
sum.sortTriple(sum.triple, 1, count);// 对输入的三元组排序
return sum;
}

// 计算两个三元组表示的矩阵之和,返回结束
private static int[][] addMatrix(TripleSMatrix t1, TripleSMatrix t2) {
if (t1.getRows() != t2.getRows() || t1.getColumns() != t2.getColumns()) {
System.out.println("这两个矩阵不能相加 。");
return null;
}
int[][] c = new int[t1.getRows()][t2.getColumns()];
int i = 1, j = 1;
while (i <= t1.getNonzeroElements() && j <= t2.getNonzeroElements()) {
if (t1.triple[i][0] < t2.triple[j][0]
&& i <= t1.getNonzeroElements()) {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
} else if (t1.triple[i][0] > t2.triple[j][0]
&& j < t2.getNonzeroElements()) {
c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t2.triple[i][2];
j++;
} else {
if (t1.triple[i][1] < t2.triple[j][1]
&& i <= t1.getNonzeroElements()) {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
} else if (t1.triple[i][1] > t2.triple[j][1]
&& j <= t2.getNonzeroElements()) {
c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t1.triple[i][2];
j++;
} else {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2]
+ t2.triple[j][2];
i++;
j++;
}
}
}

if (i > t1.getNonzeroElements()) {
while (j <= t2.getNonzeroElements()) {
c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t2.triple[i][2];
j++;
}
} else if (j > t2.getNonzeroElements()) {
while (i <= t1.getNonzeroElements()) {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
}
}
return c;
}
}
gmloveshenxue 2009-06-24
  • 打赏
  • 举报
回复
很好,比我强大!
kljiao87 2009-06-23
  • 打赏
  • 举报
回复
學習 还有其他方法吗
goodmrning 2009-06-23
  • 打赏
  • 举报
回复
学习!
zbwxmu 2009-06-23
  • 打赏
  • 举报
回复
很强大啊
kingssman 2009-06-23
  • 打赏
  • 举报
回复
学习学习
jacaranda 2009-06-23
  • 打赏
  • 举报
回复
不错不错 ,学习
L0320 2009-06-23
  • 打赏
  • 举报
回复
真的有点难度,我看我要好好学习一下
yangkaixin1226 2009-06-23
  • 打赏
  • 举报
回复
学习一下
bigbug9002 2009-06-23
  • 打赏
  • 举报
回复

稀疏矩阵减法的算法和程序中的加法想似,时间复杂度都是O(n+m),n,m分别表示两个矩阵的非零元素个数.

稀疏矩阵乘法的算法稍复杂一点,<c语言版数据结构>严蔚敏等编,清华大学出版社的第100页有相关算法的伪C++代码.
如果要在3楼的代码基础上实现,要对TripleSMatrix类做一点点改进,加一个数组属性,数组中存放每一行的第一个非零元素的下标.

希望楼主能成功写出减法和乘法的代码.
wenkyxl 2009-06-23
  • 打赏
  • 举报
回复
好复杂的代码啊!很想快点学好这些.
bigbug9002 2009-06-23
  • 打赏
  • 举报
回复
import java.util.*;
//稀疏矩阵算法。
//稀疏矩阵算法是为了在大型矩阵中非零元素少时,减少存贮空间,并提高矩阵运算速度的。
//但本例中的矩阵只是为了演示算法,都比较小,时间和空间效率提升可以忽略。
public class SparseMatrix{
public static void main(String[] args){
TripleSMatrix tsm=new TripleSMatrix(7,4);
//tsm.printTriple();
tsm.printMatrix();
TripleSMatrix tsm2=new TripleSMatrix(7,4);
System.out.println("矩阵a:");
tsm.printMatrix();
System.out.println("矩阵b:");
tsm2.printMatrix();
int[][] matrixSum=addSMatrix(tsm,tsm2);
System.out.println("矩阵a+矩阵b:");
for(int i=0;i<matrixSum.length;i++){
for(int j=0;j<matrixSum[i].length;j++){
System.out.print(" "+matrixSum[i][j]);
}
System.out.println("");
}
}
public static int[][] addSMatrix(TripleSMatrix t1,TripleSMatrix t2){ //计算两个三元组表示的矩阵之和,返回结束数组
if(t1.rows!=t2.rows||t1.columns!=t2.columns){
System.out.println("这两个矩阵不能相加");
return null;
}
int[][] c=new int[t1.rows][t2.columns];
int i=1,j=1;
while(i<=t1.nonzeroElements||j<=t2.nonzeroElements){
if(t1.triple[i][0]<t2.triple[j][0]&&i<=t1.nonzeroElements){
c[t1.triple[i][0]-1][t1.triple[i][1]-1]=t1.triple[i][2];
i++;
}else if(t2.triple[j][0]<t1.triple[i][0]&&j<=t2.nonzeroElements){
c[t2.triple[j][0]-1][t2.triple[j][1]-1]=t2.triple[j][2];
j++;
}else{
if(t1.triple[i][1]<t2.triple[j][1]&&i<=t1.nonzeroElements){
c[t1.triple[i][0]-1][t1.triple[i][1]-1]=t1.triple[i][2];
i++;
}else if(t1.triple[i][1]>t2.triple[j][1]&&j<=t2.nonzeroElements){
c[t2.triple[j][0]-1][t2.triple[j][1]-1]=t2.triple[j][2];
j++;
}else{
c[t1.triple[i][0]-1][t1.triple[i][1]-1]=t1.triple[i][2]+t2.triple[j][2];
i++;j++;
}

}
}
return c;
}
}
//下面的类定义不一定是最好的,比如其中的属性大多是包访问权限,可以改进。
class TripleSMatrix{ //定义了一个三元组的类。
int[][] triple=new int[2001][3]; //三元组数组,假设稀疏矩阵的值都是整数。最多可以有2000个非零元素。第零行没有用。
int rows,columns,nonzeroElements; //稀疏矩阵的行列数和非零元素个数。
TripleSMatrix(int rows,int columns){ //构造方法,rows是稀疏矩阵的行数,columns是稀疏矩阵的列数。
Scanner input=new Scanner(System.in);
System.out.println("请输入稀疏矩阵三元组");
System.out.println("以行 列 值的形式输入,如:1 2 4表示第1行第2列元素的值为4,当输入的行为999时结束:");
int count=1;
int i=0,j,v; //i行j列,值v
while(i!=999&&input.hasNext()){
i=input.nextInt();
j=input.nextInt();
v=input.nextInt();
if(i>rows||i<1||j>columns||j<1){
System.out.println("刚才的行,列值错,将被忽略");
continue;
}
triple[count][0]=i;
triple[count][1]=j;
triple[count][2]=v;
count++;
}
this.rows=rows;
this.columns=columns;
this.nonzeroElements=count-1;
sortTriple(triple,1,count); //对输入的三元组排序。
}

static void sortTriple(int[][] triple,int first,int end){ //对三元组排序方法,按行排,行一样按列排。
Arrays.sort(triple,first,end,new Comparator<int[]>(){
public int compare(int[] t1,int[] t2){
if(t1[0]>t2[0]) return 1;
if(t1[0]<t2[0]) return -1;
if(t1[0]==t2[0]) return t1[1]-t2[1];
return 0; //没有用的一个语句,但没有它编译通不过。
}
});
}
public void printMatrix(){ //打印出当前三元组表示的稀疏矩阵。
int row=1,column=1; //row当前要打印的行,column当前要打印的列。
for(int t=1;t<=nonzeroElements;t++){
while(triple[t][0]>row){ //三元组中的行比当前行大
if(column!=1){ //前面打印的行没有打印完,继续打印完
for(;column<=columns;column++) System.out.print(" "+0);
column=1; //新的一行列从1开始。
}else{ //当前行全为0
for(int i=1;i<=columns;i++){
System.out.print(" "+0);
}
}
System.out.println(""); //换行
row++; //下一行
}
for(;column<triple[t][1];column++){ //当前打印的列小于三元组中的列,前面要补零。
System.out.print(" "+0);
}
System.out.print(" ".substring(0,6-(String.valueOf(triple[t][2])).length())+triple[t][2]); //打印三元组对应的元素。
column++;
}
if(column!=1){ //前面打印的行没有打印完,继续打印完
for(;column<=columns;column++) System.out.print(" "+0);
System.out.println("");
column=1;
row++ ;
}
for(;row<=rows;row++){ //三元组中没有对应的值了,矩阵后面的元素全为0
for(column=1;column<=columns;column++){
System.out.print(" "+0);
}
System.out.println("");
}
}
public void printTriple(){ //打印三元组
for(int i=1;i<=nonzeroElements;i++){
for(int j=0;j<3;j++){
System.out.print(triple[i][j]+" ");
}
System.out.println("");
}
}
}
bigbug9002 2009-06-23
  • 打赏
  • 举报
回复
import java.util.*;
//稀疏矩阵算法。
//稀疏矩阵算法是为了在大型矩阵中非零元素少时,减少存贮空间,并提高矩阵运算速度的。
//但本例中的矩阵只是为了演示算法,都比较小,时间和空间效率提升可以忽略。
public class SparseMatrix{
public static void main(String[] args){
TripleSMatrix tsm=new TripleSMatrix(7,4);
//tsm.printTriple();
tsm.printMatrix();
TripleSMatrix tsm2=new TripleSMatrix(7,4);
System.out.println("矩阵a:");
tsm.printMatrix();
System.out.println("矩阵b:");
tsm2.printMatrix();
int[][] matrixSum=addSMatrix(tsm,tsm2);
System.out.println("矩阵a+矩阵b:");
for(int i=0;i<matrixSum.length;i++){
for(int j=0;j<matrixSum[i].length;j++){
System.out.print(" "+matrixSum[i][j]);
}
System.out.println("");
}
}
public static int[][] addSMatrix(TripleSMatrix t1,TripleSMatrix t2){ //计算两个三元组表示的矩阵之和,返回结束数组
if(t1.rows!=t2.rows||t1.columns!=t2.columns){
System.out.println("这两个矩阵不能相加");
return null;
}
int[][] c=new int[t1.rows][t2.columns];
int i=1,j=1;
while(i<=t1.nonzeroElements||j<=t2.nonzeroElements){
if(t1.triple[i][0]<t2.triple[j][0]&&i<=t1.nonzeroElements){
c[t1.triple[i][0]-1][t1.triple[i][1]-1]=t1.triple[i][2];
i++;
}else if(t2.triple[j][0]<t1.triple[i][0]&&j<=t2.nonzeroElements){
c[t2.triple[j][0]-1][t2.triple[j][1]-1]=t2.triple[j][2];
j++;
}else{
if(t1.triple[i][1]<t2.triple[j][1]&&i<=t1.nonzeroElements){
c[t1.triple[i][0]-1][t1.triple[i][1]-1]=t1.triple[i][2];
i++;
}else if(t1.triple[i][1]>t2.triple[j][1]&&j<=t2.nonzeroElements){
c[t2.triple[j][0]-1][t2.triple[j][1]-1]=t2.triple[j][2];
j++;
}else{
c[t1.triple[i][0]-1][t1.triple[i][1]-1]=t1.triple[i][2]+t2.triple[j][2];
i++;j++;
}

}
}
return c;
}
}
//下面的类定义不一定是最好的,比如其中的属性大多是包访问权限,可以改进。
class TripleSMatrix{ //定义了一个三元组的类。
int[][] triple=new int[2001][3]; //三元组数组,假设稀疏矩阵的值都是整数。最多可以有2000个非零元素。第零行没有用。
int rows,columns,nonzeroElements; //稀疏矩阵的行列数和非零元素个数。
TripleSMatrix(int rows,int columns){ //构造方法,rows是稀疏矩阵的行数,columns是稀疏矩阵的列数。
Scanner input=new Scanner(System.in);
System.out.println("请输入稀疏矩阵三元组");
System.out.println("以行 列 值的形式输入,如:1 2 4表示第1行第2列元素的值为4,当输入的行为999时结束:");
int count=1;
int i=0,j,v; //i行j列,值v
while(i!=999&&input.hasNext()){
i=input.nextInt();
j=input.nextInt();
v=input.nextInt();
if(i>rows||i<1||j>columns||j<1){
System.out.println("刚才的行,列值错,将被忽略");
continue;
}
triple[count][0]=i;
triple[count][1]=j;
triple[count][2]=v;
count++;
}
this.rows=rows;
this.columns=columns;
this.nonzeroElements=count-1;
sortTriple(triple,1,count); //对输入的三元组排序。
}

static void sortTriple(int[][] triple,int first,int end){ //对三元组排序方法,按行排,行一样按列排。
Arrays.sort(triple,first,end,new Comparator<int[]>(){
public int compare(int[] t1,int[] t2){
if(t1[0]>t2[0]) return 1;
if(t1[0]<t2[0]) return -1;
if(t1[0]==t2[0]) return t1[1]-t2[1];
return 0; //没有用的一个语句,但没有它编译通不过。
}
});
}
public void printMatrix(){ //打印出当前三元组表示的稀疏矩阵。
int row=1,column=1; //row当前要打印的行,column当前要打印的列。
for(int t=1;t<=nonzeroElements;t++){
while(triple[t][0]>row){ //三元组中的行比当前行大
if(column!=1){ //前面打印的行没有打印完,继续打印完
for(;column<=columns;column++) System.out.print(" "+0);
column=1; //新的一行列从1开始。
}else{ //当前行全为0
for(int i=1;i<=columns;i++){
System.out.print(" "+0);
}
}
System.out.println(""); //换行
row++; //下一行
}
for(;column<triple[t][1];column++){ //当前打印的列小于三元组中的列,前面要补零。
System.out.print(" "+0);
}
System.out.print(" ".substring(0,6-(String.valueOf(triple[t][2])).length())+triple[t][2]); //打印三元组对应的元素。
column++;
}
if(column!=1){ //前面打印的行没有打印完,继续打印完
for(;column<=columns;column++) System.out.print(" "+0);
System.out.println("");
column=1;
row++ ;
}
for(;row<=rows;row++){ //三元组中没有对应的值了,矩阵后面的元素全为0
for(column=1;column<=columns;column++){
System.out.print(" "+0);
}
System.out.println("");
}
}
public void printTriple(){ //打印三元组
for(int i=1;i<=nonzeroElements;i++){
for(int j=0;j<3;j++){
System.out.print(triple[i][j]+" ");
}
System.out.println("");
}
}
}
Korbin Luo 2009-06-23
  • 打赏
  • 举报
回复
无论如何,佩服bigbug9002.我先学习一下.
cydljg 2009-06-23
  • 打赏
  • 举报
回复
手机批量出货(质量问题包退换)
NOKIA N97i-----5XX元
NOKIA N95------3XX元
NOKIA N96------4XX元
NOKIA 8800-----5XX元
NOKIA 8800-----4XX元(无缝精品下滑盖)
NOKIA N83 -----4XX元(时尚纯屏语音王)
NOKIA E66 -----5XX元(黑白色男女精品)
法拉利TI-------7XX元(世界最尊贵的机)


网 址:www.sina898.com
Q Q: 417987690 80532866
电话: 0755-29980520 13410028731 13530565114
欢迎:识货有量或寻找质量稳定货源的朋友来人来电联系
加QQ索取相关资料
yl0503 2009-06-23
  • 打赏
  • 举报
回复
等待高手!!
dukeng 2009-06-23
  • 打赏
  • 举报
回复
等待高手!!
bigbug9002 2009-06-23
  • 打赏
  • 举报
回复
一次运行结果如下:
F:\>cd java

F:\java>java SparseMatrix
请输入稀疏矩阵三元组
以行 列 值的形式输入,如:1 2 4表示第1行第2列元素的值为4,当输入的行为999时结束:
5 2 22
4 1 40
7 4 -8
4 3 3
5 2 1
4 4 1
2 3 2
999 9 9
刚才的行,列值错,将被忽略
请输入稀疏矩阵三元组
以行 列 值的形式输入,如:1 2 4表示第1行第2列元素的值为4,当输入的行为999时结束:
2 2 33
1 2 20
3 4 -2
6 3 13
5 2 14
7 4 6
999 9 9
刚才的行,列值错,将被忽略
矩阵a:
0 0 0 0
0 0 2 0
0 0 0 0
40 0 3 1
0 22 1 0
0 0 0 0
0 0 0 -8
矩阵b:
0 20 0 0
0 33 0 0
0 0 0 -2
0 0 0 0
0 14 0 0
0 0 13 0
0 0 0 6
矩阵a+矩阵b:
0 20 0 0
0 33 2 0
0 0 0 -2
40 0 3 1
0 1 0 0
0 0 13 0
0 0 0 -2

F:\java>
加载更多回复(2)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧