6 喜歡 1 不喜歡
2.4k 瀏覽
如果在矩陣中,多數的元素並沒有資料,稱此矩陣為稀疏矩陣(sparse matrix),由於矩陣在程式中常使用二維陣列表示,二維陣列的大小與使用的記憶體空間成正比,如果多數的元素沒有資料,則會造成記憶體空間的浪費,為 此,必須設計稀疏矩陣的陣列儲存方式,利用較少的記憶體空間儲存完整的矩陣資訊。

在這邊所介紹的方法較為簡單,陣列只儲存矩陣的行數、列數與有資料的索引位置及其值,在需要使用矩陣資料時,再透過程式運算加以還原,例如若矩陣資料如下 ,其中0表示矩陣中該位置沒有資料:

0 0 0 0 0 0
0 3 0 0 0 0
0 0 0 6 0 0
0 0 9 0 0 0
0 0 0 0 12 0

這個矩陣是5X6矩陣,非零元素有4個,您要使用的陣列第一列記錄其列數、行數與非零元素個數:

5 6 4

陣列的第二列起,記錄其位置的列索引、行索引與儲存值:

1 1 3
2 3 6
3 2 9
4 4 12

所以原本要用30個元素儲存的矩陣資訊,現在只使用了15個元素來儲存,節省了不少記憶體的使用。

 

請寫一個程式用上列的方法壓縮稀疏矩陣。

 

輸入說明:

一開始會輸入兩個正整數M N,代表矩陣的大小。接下來會有M行,每行N個數字。

 

輸出說明:

請輸出壓縮過的矩陣

 

輸入範例:

5 6

0 0 0 0 0 0
0 3 0 0 0 0
0 0 0 6 0 0
0 0 9 0 0 0
0 0 0 0 12 0

 

輸出範例:

5 6 4
1 1 3
2 3 6
3 2 9
4 4 12
[考試] 最新提問 分類:2017-1 程式設計(一)AD | 用戶: (18k 分)
ID: 36176 - 從幾時開始: 2017-12-08 18:30 - 到幾時結束: 2017-12-08 21:00
| 2.4k 瀏覽

6 個回答

0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
int main(void)
{
** *** ***** * * * **** A[m][n];
* ** ******* * * * ** * * *** * *** * ** ***
** **** *** ** **** ** * * * *** *** *** *
** **** ***** * **** * * *
** * * ******* * * *
** * **** ****** ** * * *****
* *** ** **** * * *** *
* *** * ** *** ** ** * * * * * ***** ****** **
* * **** ***** * *
*** * ***** ** * ** * * * * * ** * *
* ***** * * ** *
   
    return 0; ** * ** * *** * ** * * ** * * ** *
 }
最新回答 用戶: (144 分)
0 0
prog.c: In function 'main':
prog.c:4:11: error: 'm' undeclared (first use in this function)
     int A[m][n];
           ^
prog.c:4:11: note: each undeclared identifier is reported only once for each function it appears in
prog.c:4:14: error: 'n' undeclared (first use in this function)
     int A[m][n];
              ^
prog.c:11:23: error: expected expression before ')' token
     printf("%d%d%d\n",)
                       ^
0 0
prog.c: In function 'main':
prog.c:4:11: error: 'm' undeclared (first use in this function)
     int A[m][n];
           ^
prog.c:4:11: note: each undeclared identifier is reported only once for each function it appears in
prog.c:4:14: error: 'n' undeclared (first use in this function)
     int A[m][n];
              ^
prog.c:11:23: error: expected expression before ')' token
     printf("%d%d%d\n",)
                       ^
0 喜歡 0 不喜歡
內容已隱藏
#include<stdio.h>

int main(){
    
** ** *** *** * **** ** a,b,x,y,i=0;
**** ***** ** * *** * ** %d",&a,&b);
    
* *** *** * ** ** ** * * ch[a][b];
    
** * * * ** * * ** * * * *
* *** ** ****** ** * * * * * **** * * ** ** ** * *
*** ** * * ********* **** * **** * * ** ** ******* ** * **** * *** * * **********
* * **** * ** ****** * * * *** * * ** * **** ** * ** ** * **
* *** ** * ** *** *** * ** ** * * * * *** ** * **
* **** ** ** ** *** **** * * * **** *** *** * * *
** * ** * ** * ** * *** ** * * * *
**** ****** ** * * * ** * * * %d %d\n",a,b,i);
** *** * ** * * * ****** * *
**** ** * **** ** **** ** * ** ** * *** ** ** * ****
* * * * * *** * ** * * * ** *** * ** ** ** * * * ** *
* ** **** ** * * *** *** ** ** * ** * * ** ***** * * * **** ** %d %d\n",x,y,ch[x][y]);}
** * * * ** * * *** * ** ** * *** *** * **** *
* ***** *** ***** * * * *** ** * **
** * * **** * * * * system("pause");
}
最新回答 用戶: (192 分)
修改於 用戶:
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
0 喜歡 0 不喜歡
內容已隱藏
*** *** ** ** *****
最新回答 用戶: (131 分)
0 0
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
0 喜歡 0 不喜歡
內容已隱藏
*** ** * *** * **
int main ()
{
* * ** * * ** **** *** M,N;
** * * ** *** ** ** *** ****** * **
* * ** * * ** * ** ****** ** %d",M,N);
* * * * **** **** *
*** * * ******* ** **** ** 0;
}
最新回答 用戶: (215 分)
0 0
Case 0: Wrong output
Case 1: Wrong output
0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>

int main()
{
    int row,col;
    while(scanf("%d %d",&row,&col)!=EOF)
    {
* * *** **** * * * ***** *** ***** array[row][col];
***** * ** ***** * ** * ** * * ** ** *** * ***** save[row*col][3];
*** ** * *** * **** **** ** ** *** *** * * * * i,j,count=0;
*** * * * * * * *** * * *** * ** ** * *** ** * * ** *** ** *
        {
** ** * * * * * *** **** * ** *** * * ** ** *** * ** *** ** **** ** * * *** ** ** ** *
** * * *** * ******* **** * **** ** * ** ** * *** ** ** ***** *** * * * *** * * ** * * ******** *** *
        }
* *** **** * *** ** *** * **** * * * *** ** * *** ** ** *
        {
** * * * * * * * ***** *** ** *** * * *** ** * ***** * ** * *** *** * *
** *** ** ** * * * ***** * * *** * ** * * ***** *** ** * *
* *** *** * * * ** * ** **** * * ** ** * * **** * ** *** ** *** * *** * * * ** ***
* * * ** *** * **** * *** *** * * ** * **** * * ** ** *** * ** * *** **** * **** *** *
* * * ** * ** * * ** **** * ******* ** * *** ****** ******* *** ** * ** *** **** *** * * * *** ** *
*** ** * *** *** ** ****** ** * **** *** * * ** * ** * ** * *** * * ** *** **** * ****** * * ** * * * * * *
*** *** * * * * *** * ** ** ***** ** * * * * **** * * *** ****** ** *** ******** * * **** * ** *** *** * ** * ***** *
* **** * * ** * *** ** ** * *** ** * ** * **** * * * * ** ***** * * *** ******* * * ***** * *** **
** * * *** * *** * * ** **** *** * *** ** * **** *** **** ** ****** ** ***** ** * ** * *
** * ** ** * **** ** * ** ** * * ** * ** ** * ** ** **** *
* * *** ** *** * * **** *** * ***** ** * * *
* **** *** * ** *** ** * * * **** ** ******* ** %d %d\n",row,col,count);
* * * ** ** ** *** ******* ** * * * * ** * *** *
        {
** *** ** ** *** * * *** * *** * * * ****** ** ** *** ****** ** **** * ** * ** ** **
* **** * * * * * * * ** * * ** * * **** * * *** * * * **** ***** **** ** * * * **** *** ** * *** *** ",save[i][j]);
* * * **** ** ** ** ***** * * * **** **** * *** ****** * ** * * * *** * * * **** * * *
* * * * * ** * * ** * * * * * * * ****

    }

    return 0;
}
最新回答 用戶: (114 分)
修改於 用戶:
0 0
prog.c:1:1: error: expected identifier or '(' before numeric constant
 0 0 0 0 0 0
 ^
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
0 喜歡 0 不喜歡
內容已隱藏
#include <stdio.h>
int main()
{
** *** * ** * * ***** * * *** a,b;
** * * ** * * *** * * ** * * %d",&a,&b);
** * * * ***** * ***** * arr[a][b];
    int i,j,count=0;
* *** * * ** *** ** **********
    {
* * **** ** * * ** * **** * * **** * *** ** ********
* * ** ** *** *** * **** *** * * *
* * * * ** * * ******** ** ** ** ** ** ** * * ** *** * * ***** ** **
**** ** * * * * *** * * * ***** ** ** * * ** ** * * * **** * ** ** * * * **** * **
********* * * **** * * * * *** ** ***** *
    }

** **** * * * *** ** **** **
    {
* *** * *** ** ****** * **** * * ** * * ** *** *** * ** * *
** * **** ** **** *** * * * * ****
*** * *** ** * * ** * **** **** * * ***** * *** * * * * ** ** ** * ** ** *
* ** * * * *** ** ** * * *** * * ****** * ****** * * * * ** * *** *** ** * *** ***
* * ** * *** **** ** ** ** * * * ** * ****
    }
* * ** * * * ** ****** ***** * ** %d %d\n",a,b,count);
* *** * * *** * * * ** * **** *****
    {
*** * * **** * * * **** **** *** * ** * ****** ***
** * ** * * ***** ** *** * * * * **** **
** **** *** ** ** ** * **** ****** * * * * ****** ** * * * * * * ****
* **** ** * * *** * ** ***** * *** ** ** * * * * ** * ** * ** * ** * * * * * * * ** * * %d %d\n",i,j,arr[i][j]);
** * * ***** * * * * ** * ** ** * *
    }
* ** * **** ***** * * * *** 0;
}
最新回答 用戶: (100 分)
1 0
Case 0: Correct output
Case 1: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.7.77
©2016-2025

相關問題

3 喜歡 0 不喜歡
5 回答
[正常] Coding (C) - 最新提問 12月 20, 2017 分類:2017-1 程式設計(一)AD | 用戶: 楊修俊 (30k 分)
ID: 38153 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 2.3k 瀏覽
2 喜歡 0 不喜歡
5 回答
[考試] 最新提問 12月 23, 2017 分類:2017-1 程式設計(一)AD | 用戶: 楊修俊 (30k 分)
ID: 38942 - 從幾時開始: 2017-12-23 09:00 - 到幾時結束: 2017-12-23 12:10
| 2.8k 瀏覽
2 喜歡 0 不喜歡
18 回答
[考試] 最新提問 12月 23, 2017 分類:2017-1 程式設計(一)AD | 用戶: 楊修俊 (30k 分)
ID: 38941 - 從幾時開始: 2017-12-23 09:00 - 到幾時結束: 2017-12-23 12:10
| 7.3k 瀏覽
12,783 問題
183,442 回答
172,219 留言
4,824 用戶