学校作业~~急~~请用C++写

Home Home
引用 | 编辑 y2123280394
2006-06-01 04:52
楼主
推文 x0
宣告int a,b,c
1. 输入m,n,p<10 ..

访客只能看到部份内容,免费 加入会员



献花 x0
引用 | 编辑 pkpk0808
2006-06-03 17:52
1楼
  
C=AB 没错吧?
我用C写的,除了matrix mutiply的演算法要想一下,其他应该都不难
输出输入要改再自己改啰~

复制程式
#include <stdio.h>

int main()
{
       int m, n, p;
       int i, j, k;

       int a[10][10], b[10][10], c[10][10];

       /* input m, n, p */
       scanf("%d %d %d", &m, &n, &p);

       /* input A */
       for(i=0; i<m; i++)
              for(j=0; j<n; j++)
                     scanf("%d", &a[i][j]);

       /* input B */
       for(i=0; i<n; i++)
              for(j=0; j<p; j++)
                     scanf("%d", &b[i][j]);

       /* initialize C */
       for(i=0; i<m; i++)
              for(j=0; j<p; j++)
                     c[i][j] = 0;
       
       /* multiple */
       for(i=0; i<m; i++)
              for(j=0; j<p; j++)
                     for(k=0; k<n; k++)
                            c[i][j] = c[i][j] + a[i][k]*b[k][j];

       printf("\n");
       /* display C */
       for(i=0; i<m; i++)
       {
              for(j=0; j<p; j++)
                     printf("%4d", c[i][j]);
              printf("\n");
       }

       return 0;
}




sample input 1:
复制程式
3 2 3

5 1
3 4
2 6

2 2 1
6 4 5



sample output 1:
复制程式
  16  14  10
  30  22  23
  40  28  32



sample input 2:
复制程式
3 3 3

1 2 5
0 1 4
2 1 1

1 3 2
2 3 4
1 5 1



sample output 2:
复制程式
  10  34  15
   6  23   8
   5  14   9


献花 x0
引用 | 编辑 y2123280394
2006-06-03 22:33
2楼
  
这是我同学用C++打的

以下是代码:

#include<iostream.h>
#include<stdio.h>
void main()
{
     int a[10][10],b[10][10],c[10][10]={0},m,n,p,i,j,k;
{
cout<<" m n p"<<endl;
cin>>m>>n>>p;
}
while(!(n<10 && m<10 && p<10))
{
cout<<"请重新输入 m n p"<<endl;
cin>>m>>n>>p;
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
cout<<"a["<<i<<"]"<<"["<<j<<"]=";
cin>>a[j];
}
}

  for(i=1;i<=n;i++)
{
for(j=1;j<=p;j++)
{
cout<<"b["<<i<<"]"<<"["<<j<<"]=";
cin>>b[j];
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=p;j++)
{
for(k=1;k<=n;k++)
c[j]=c[j]+a[k]+b[k][j];
cout<<"c["<<i<<"]"<<"["<<j<<"]="<<c[j]<<"\t";
}
}
}
执行结果:
以下是代码:
m n p
2
2
2
a[1][1]=2
a[1][2]=2
a[2][1]=2      
a[2][2]=2
b[1][1]=2              
b[1][2]=2            
b[2][1]=2                  
b[2][2]=2
c[1][1]=8     c[1][2]=8     c[2][1]=8     c[2][2]=8    
Press any key to continue
老师要的是
以下是代码:
m n p
2
2
2
a[1][1]=2             a[1][2]=2            a[2][1]=2                  a[2][2]=2
b[1][1]=2             b[1][2]=2            b[2][1]=2                  b[2][2]=2
c[1][1]=8             c[1][2]=8           c[2][1]=8                       c[2][2]=8    
Press any key to continue
怎么改呢?各为高手教教我吧

献花 x0