利用C++實現(xiàn)矩陣的相加/相稱/轉(zhuǎn)置/求鞍點
更新時間:2013年10月21日 08:49:37 作者:
利用C++實現(xiàn)矩陣的相加/相稱/轉(zhuǎn)置/求鞍點。需要的朋友可以過來參考下,希望對大家有所幫助
1.矩陣相加
兩個同型矩陣做加法,就是對應(yīng)的元素相加。
復(fù)制代碼 代碼如下:
#include<iostream>
using namespace std;
int main(){
int a[3][3]={{1,2,3},{6,5,4},{4,3,2}};
int b[3][3]={{4,3,2},{6,5,4},{1,2,3}};
int c[3][3]={0,0,0,0,0,0,0,0,0};
int i,j;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]+=a[i][j];//實現(xiàn)相加操作1
cout<<"\t"<<a[i][j];//輸出矩陣A
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]+=b[i][j];//實現(xiàn)矩陣操作2
cout<<"\t"<<b[i][j];//輸出矩陣B
}
cout<<endl;
}
cout<<endl;
cout<<"Array C:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cout<<"\t"<<c[i][j];//輸出矩陣C
}
cout<<endl;
}
cout<<endl;
return 0;
}

2.實現(xiàn)矩陣的轉(zhuǎn)置
復(fù)制代碼 代碼如下:
#include<iostream>
using namespace std;
int main(){
int a[3][2]={{4,3},{6,5},{1,2}};
int b[2][3]={0,0,0,0,0,0};
int i,j;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<2;j++){
cout<<"\t"<<a[i][j];//輸出矩陣A
b[j][i]=a[i][j];//進行轉(zhuǎn)置操作
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<"\t"<<b[i][j];
}
cout<<endl;
}
cout<<endl;
return 0;
}
3.實現(xiàn)矩陣的相乘
一個m行n列的矩陣可以和n列k行的矩陣相乘,得到一個m行k列的矩陣
復(fù)制代碼 代碼如下:
#include<iostream>
using namespace std;
int main(){
int a[3][2]={{4,3},{6,5},{1,2}};
int b[2][3]={{1,2,3},{6,5,4}};
int c[3][3]={0,0,0,0,0,0,0,0,0};
int i,j,k,l;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<2;j++){
cout<<"\t"<<a[i][j];//輸出矩陣A
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<"\t"<<b[i][j];//輸出矩陣B
}
cout<<endl;
}
cout<<endl;
cout<<"Array C:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(k=0;k<2;k++){
c[i][j]+=a[i][k]*b[k][j];//實現(xiàn)相乘操作
}
cout<<"\t"<<c[i][j];//輸出矩陣C
}
cout<<endl;
}
cout<<endl;
return 0;
}
4.求矩陣中的鞍點
在矩陣中行中最大,列中最小的元素就是我們要求的鞍點
復(fù)制代碼 代碼如下:
#include<iostream>
using namespace std;
int main(){
int a[3][4]={{3,2,13,1},{8,7,10,5},{12,11,14,9}};
int i,j,k,ad,q=0;
bool tag;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
cout<<endl;
for(i=0;i<3;i++){
ad=a[i][0];
tag=true;
for(j=1;j<4;j++){
if(ad<a[i][j]){
k=j;
}//先選出行中最大
}
for(j=0;j<3;j++){
if(a[i][k]>a[j][k]){
tag=false;
};//再選出列中最小
}
cout<<endl;
if(tag==true){
cout<<"鞍點是第"<<(i+1)<<"行,第"<<(k+1)<<"列的"<<a[i][k]<<endl;
q++;
}
}
if(q==0){
cout<<"沒有一個鞍點~"<<endl;
}
cout<<endl;
return 0;
}
相關(guān)文章
C++實現(xiàn)stack與queue數(shù)據(jù)結(jié)構(gòu)的模擬
stack是一種容器適配器,專門用在具有后進先出操作的上下文環(huán)境中,其刪除只能從容器的一端進行 元素的插入與提取操作;隊列是一種容器適配器,專門用于在FIFO上下文(先進先出)中操作,其中從容器一端插入元素,另一端提取元素2023-04-04
C語言編程銀行ATM存取款系統(tǒng)實現(xiàn)源碼
這篇文章主要為大家介紹了C語言編程銀行ATM存取款系統(tǒng)實現(xiàn)的源碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11

