反查找 元素的 indice。
#include <stdio.h>
#define SIZE 10
// Calculate the average value within an n x n area with (x, y) as the top-left corner
int op(int data[][SIZE], int x, int y, int n) {
int value = 0;
printf("Indices of used elements:\n"); // Display the start of a new area
for (int i = x; i < x + n; i++) { // Traverse specified rows
for (int j = y; j < y + n; j++) { // Traverse specified columns
printf("(%d, %d) ", i, j); // Print the index being used
value += data[i][j]; // Accumulate the value of data[i][j]
}
printf("\n"); // New line after each row
}
printf("\n"); // New line after each area's indices are printed
return value / (n * n); // Return the average value
}
// Print the target matrix
void print(int target[][SIZE], int size) {
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++)
printf("%d ", target[x][y]);
printf("\n");
}
printf("\n");
}
// Compress regions from the data matrix into the target matrix
void compress(int data[][SIZE], int target[][SIZE], int size, int n) {
int cSize = size / n;
for (int x = 0; x < cSize; x++) {
for (int y = 0; y < cSize; y++) {
printf("Compressing area to target[%d][%d]:\n", x, y); // Display target index of compression
target[x][y] = op(data, x * n, y * n, n); // Use op function to calculate the average
}
}
print(target, cSize); // Print the compressed target matrix
}
int main() {
int data[SIZE][SIZE] = {
{1, 2, 3, 4, 5, 6},
{3, 4, 5, 6, 7, 8},
{5, 6, 1, 2, 3, 4},
{7, 7, 9, 9, 5, 5},
{2, 2, 4, 4, 8, 8},
{1, 1, 5, 5, 9, 9}
};
int target[SIZE][SIZE];
compress(data, target, 4, 2); // Compress a 4x4 area with a 2x2 block size
compress(data, target, 6, 2); // Compress a 6x6 area with a 2x2 block size
compress(data, target, 6, 3); // Compress a 6x6 area with a 3x3 block size
return 0;
}
執行結果:
Compressing area to target[0][0]:
Indices of used elements:
(0, 0) (0, 1)
(1, 0) (1, 1)
Compressing area to target[0][1]:
Indices of used elements:
(0, 2) (0, 3)
(1, 2) (1, 3)
Compressing area to target[1][0]:
Indices of used elements:
(2, 0) (2, 1)
(3, 0) (3, 1)
Compressing area to target[1][1]:
Indices of used elements:
(2, 2) (2, 3)
(3, 2) (3, 3)
2 4
6 5
Compressing area to target[0][0]:
Indices of used elements:
(0, 0) (0, 1)
(1, 0) (1, 1)
Compressing area to target[0][1]:
Indices of used elements:
(0, 2) (0, 3)
(1, 2) (1, 3)
Compressing area to target[0][2]:
Indices of used elements:
(0, 4) (0, 5)
(1, 4) (1, 5)
Compressing area to target[1][0]:
Indices of used elements:
(2, 0) (2, 1)
(3, 0) (3, 1)
Compressing area to target[1][1]:
Indices of used elements:
(2, 2) (2, 3)
(3, 2) (3, 3)
Compressing area to target[1][2]:
Indices of used elements:
(2, 4) (2, 5)
(3, 4) (3, 5)
Compressing area to target[2][0]:
Indices of used elements:
(4, 0) (4, 1)
(5, 0) (5, 1)
Compressing area to target[2][1]:
Indices of used elements:
(4, 2) (4, 3)
(5, 2) (5, 3)
Compressing area to target[2][2]:
Indices of used elements:
(4, 4) (4, 5)
(5, 4) (5, 5)
2 4 6
6 5 4
1 4 8
Compressing area to target[0][0]:
Indices of used elements:
(0, 0) (0, 1) (0, 2)
(1, 0) (1, 1) (1, 2)
(2, 0) (2, 1) (2, 2)
Compressing area to target[0][1]:
Indices of used elements:
(0, 3) (0, 4) (0, 5)
(1, 3) (1, 4) (1, 5)
(2, 3) (2, 4) (2, 5)
Compressing area to target[1][0]:
Indices of used elements:
(3, 0) (3, 1) (3, 2)
(4, 0) (4, 1) (4, 2)
(5, 0) (5, 1) (5, 2)
Compressing area to target[1][1]:
Indices of used elements:
(3, 3) (3, 4) (3, 5)
(4, 3) (4, 4) (4, 5)
(5, 3) (5, 4) (5, 5)
3 5
4 6
看的出來是,
使用自己寫的compress()後
,會把原本的
方陣的元素,取平均值後,
傳入,新方陣的元素中。
而,n表示的是
被壓縮的小方陣
的邊長。
ㅤㅤ
ㅤㅤ
在 `compress()` 函數中:
- `int size` 是要壓縮的方陣的邊長,表示整個資料矩陣的大小(例如,4x4或6x6)。
- `int n` 是被壓縮的小方陣的邊長,表示每個區域的大小(例如,2x2或3x3)。
- `int cSize` 是壓縮後的目標矩陣的大小,計算方式是 `size / n`,即壓縮後的方陣邊長。
因此,允許在不同的區域大小下進行壓縮。
ㅤㅤ
ㅤㅤ