题目1489:计算两个矩阵的乘积

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lovely_girl1126/article/details/76401227

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:6000

解决:1301

题目描述:

计算两个矩阵的乘积,第一个是2*3,第二个是3*2

输入:

输入为两个矩阵,其中一个为2*3的矩阵,另一个为3*2的矩阵

输出:

一个2*2的矩阵(每一个数字后都跟一个空格)

样例输入:
1 2 3
3 4 5
6 7
8 9
10 11
样例输出:
52 58
100 112
题目没说多组输入啊,在输入时需要循环, 注意结果是每个数字后面都需要一个空格
#include<cstdio>
#include<iostream>
using namespace std;
int main() {
	int a[2][3],b[3][2];
	int c[2][2];
	while(~scanf("%d%d%d",&a[0][0],&a[0][1],&a[0][2])) {
		for(int i=1; i<2; i++) {
			for(int j=0; j<3; j++) {
				scanf("%d",&a[i][j]);
			}
		}
		for(int i=0; i<3; i++) {
			for(int j=0; j<2; j++) {
				scanf("%d",&b[i][j]);
			}
		}
		for(int i=0; i<2; i++) {
			for(int j=0; j<2; j++) {
				int t=0;
				for(int k=0; k<3; k++) {
					t+=a[i][k]*b[k][j];
				}
				c[i][j]=t;
			}
		}
		for(int i=0; i<2; i++) {
			printf("%d %d \n",c[i][0],c[i][1]);
		}
	}
	return 0;
}


展开阅读全文

单循环链表计算多项式乘积

09-13

[code=C/C++]rn#include rn#include rnusing namespace std;rnclass Nodernrnpublic:rn double coef; //---系数rn int expo; //---指数rn Node * next;rn Node():coef(0),expo(0),next(NULL) //---默认构造函数rn Node(double pa,int pb):coef(pa),expo(pb) //---构造函数rn;rnrnclass Multinomial //---多项式类 单循环链表rnrnprivate:rn int length; //链表长度rnrn Node * front; //头指针rnrn static double sum; //累计和rnpublic:rn Multinomial():length(0),front(new Node) front->next = NULL ; //构造rnrn Multinomial(Node pmul[],int plength); rnrn ~Multinomial(); //析构rnrn double Algorithm(const double x,const Multinomial & P) const; //计算两多项式乘积rnrn Node * Getfront() const; //获得头指针rn;rnrndouble Multinomial::sum =0;rnrnMultinomial::Multinomial(Node pmul[],int plength)rnrn front = new Node;rn front->next = front; //单循环rn for(int i = 0;icoef = pmul[i].coef;rn s->expo = pmul[i].expo;rn s->next = front->next;rn front->next = s;rn rn length = plength;rnrnrnMultinomial::~Multinomial()rnrn Node * p = front->next;rn Node * temp;rn while(p != front)rn rn temp = p;rn p = front->next;rn delete temp;rn rn delete p;rnrnrnNode * Multinomial::Getfront() constrnrn return front;rnrnrndouble Multinomial::Algorithm(const double x,const Multinomial & P) const //---计算多项式乘积rnrn Node * pa = front->next;rn Node * pb = NULL; rnrn while(pa != front) rn rn pb = P.Getfront()->next;rn while(pb != P.Getfront())rn rn int temp = pa->expo+pb->expo;rn sum += (pa->coef)*(pb->coef)*pow(x,temp);rn pb = pb->next;rn rn pa = pa->next;rn rnrn return sum;rnrnrnvoid main()rnrn Node ea(3,4); //两节点rn Node eb(4,5);rnrn Node a[1]=ea; //储存节点的数组rn Node b[1]=eb;rnrn Multinomial x(a,1);rn Multinomial y(b,1);rn double aa;rnrn aa=x.Algorithm(1,y);rn cout< 论坛

没有更多推荐了,返回首页