C語言求任意兩個矩陣相乘的算法(不同于大部分規(guī)格固定的矩陣乘法)
創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個客戶多一個朋友!專注中小微企業(yè)官網(wǎng)定制,網(wǎng)站設(shè)計制作、做網(wǎng)站,塑造企業(yè)網(wǎng)絡(luò)形象打造互聯(lián)網(wǎng)企業(yè)效應(yīng)。結(jié)果圖如下? ?:
代碼如下:
//----- 任意兩個矩陣相乘
# include
int main (void)
{
?char ch;
?int a, b, c, d;
? printf ("此算法用于任意兩個矩陣相乘 ?\n矩陣1(a行b列)乘以矩陣2(c行d列),請您按順序輸入a、b、c、d,中間以空格隔開:\n");
?scanf ("%d %d %d %d", &a, &b, &c, &d);
?
?while (b != c)
?{
??? ?printf ("您輸入的矩陣規(guī)格不能相乘,請重新輸入。\n");
??? ?printf ("矩陣1(a行b列)乘以矩陣2(c行d列),請按順序輸入a、b、c、d,中間以空格隔開:\n");
??? ?scanf ("%d %d %d %d", &a, &b, &c, &d);
?}
?
?while ((ch = getchar()) != '\n')
??? ??? ?continue;
? printf ("\n");
?int i, j;
?int f[100][100] = {0}; //100是隨機大數(shù),是算法能計算的矩陣上限
?int?? ?g[100][100] = {0};
?int h[100][100] = {0};
?printf ("\n");
?
?for (i=0;i ?? ?{
??? ?printf ("請輸入矩陣1的第%d行數(shù)據(jù),元素之間用空格隔開:\n", (i+1));
??? ?
??? ?for (j=0;j ??? ?{
??? ??? ?scanf ("%d", &f[i][j]);
??? ?}
??? ?
??? ?while ((ch = getchar()) != '\n')
??? ??? ?continue;?? ?
?}
?
?printf ("矩陣1為:\n");
?
?for (i=0;i ?? ?{
??? ?for (j=0;j ??? ??? ?printf (" ?%-10d ", f[i][j]);//-10是指左對齊,每個數(shù)占10格
??? ?printf ("\n");
?}
?printf ("\n");
?
?for (i=0;i
??? ?printf ("請輸入矩陣2的第%d行數(shù)據(jù),元素之間用空格隔開:\n", (i+1));
??? ?for (j=0;j
??? ??? ?scanf ("%d", &g[i][j]);
??? ?}
??? ?
??? ?while ((ch = getchar()) != '\n')
??? ??? ?continue;?? ?
?}
?
?printf ("矩陣2為:\n");
?
?for (i=0;i
??? ?for (j=0;j
??? ?printf ("\n");
?}
?printf ("\n");
?
?int k;
?for (i=0;i ?? ?{
??? ?for (j=0;j
??? ??? ??? ?h[i][j] = h[i][j] + f[i][k]*g[k][j];
??? ??? ?}
?}
?
?printf ("結(jié)果矩陣為:\n");
?
?for (i=0;i ?? ?{
??? ?for (j=0;j
??? ?printf ("\n");
?}
?
?return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧