Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
71 lines (61 loc) · 1.79 KB
/
Copy pathProgram.cs
File metadata and controls
71 lines (61 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using static System.Console;
namespace MatrixSparse
{
class Program
{
static void Main(string[] args)
{
// 宣告並建立稀疏矩陣
int[,] A =
{
{ 25, 2, 0, 32, 0, -25 },
{ 0, 33, 77, 0, 0, 0 },
{ 0, 0, 0, 55, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 101, 0, 0, 0, 0, 0 },
{ 0, 0, 38, 0, 0, 0 }
};
// 建立壓縮矩陣
int[,] Compress = new int[10, 3];
// 壓縮矩陣填入
Compress[0, 0] = 6;
Compress[0, 1] = 6;
Compress[0, 2] = 9;
int i, j, tmp = 1;
for (i = 0; i < A.GetLength(0); i++)
{
for (j = 0; j < A.GetLength(1); j++)
{
if (A[i, j] != 0)
{
Compress[tmp, 0] = i + 1;
Compress[tmp, 1] = j + 1;
Compress[tmp, 2] = A[i, j];
tmp++;
}
}
}
WriteLine("\n=======稀疏矩陣 A 元素=======\n");
Print(ref A);
WriteLine("\n=======壓縮矩陣 Compress 元素=======\n");
Print(ref Compress);
ReadLine();
}
/// <summary>
/// 輸出矩陣
/// </summary>
/// <param name="matrix"></param>
static void Print(ref int[,] matrix)
{
int i;
int j;
for (i = 0; i < matrix.GetLength(0); i++)
{
for (j = 0; j < matrix.GetLength(1); j++)
Write(matrix[i, j] + "\t");
WriteLine();
}
}
}
}