使用另一个矩阵采用的元素创建一个矩阵
Create a matrix with elements taken by another matrix
我必须实现一个函数,给定一个 0 和 1 的矩阵,returns 一个包含 1 坐标的新矩阵。例如:如果矩阵是 3x3 并且输出是:
1 0 1
0 1 1
0 0 1
新矩阵为 5x2,输出为:
0 0
0 2
1 1
1 2
2 2
有什么建议吗?我的方法是这样的:
int matrix[3][3];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (matrix[i][i] == 1){
//Code i need
}
}
}
如果matrix[i][j] == 1
你对坐标[i,j]
了解多少?那他们的坐标是1
:)
其次,输入矩阵总是3x3吗?如果没有,您需要将矩阵的维度存储在变量中,并将这些变量用于 for
循环。
解决方案确实取决于要求:
- 是否允许分配尽可能大的结果矩阵(在本例中为 9 x 2)。
- 如果第1点不允许,是否严格要求使用固定大小的数组(无动态分配)。如果是这种情况,则可能需要将矩阵传递两次以分配正确大小的数组。
其他解决方案当然是使用动态分配(使用 malloc 等)。
选项1和2的简化版本如下所示:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int matrix[3][3];
matrix[0][0] = 1;
matrix[0][1] = 0;
matrix[0][2] = 1;
matrix[1][0] = 0;
matrix[1][1] = 1;
matrix[1][2] = 1;
matrix[2][0] = 0;
matrix[2][1] = 0;
matrix[2][2] = 1;
//Solution 1 - If allowed to allocate matrix with size more than the result,
//i.e. if input is 3x3 matrix, then the maximum size of result matrix is 9 x 2
int resultMatrix1[9][2];
int usedCount1=0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
resultMatrix1[usedCount1][0] = i;
resultMatrix1[usedCount1][1] = j;
usedCount1++;
} //end if
} //end for
} //end for
//Print the result
printf("\nSolution 1\n");
for (int i = 0; i < usedCount1; i++){
printf("%d %d\n", resultMatrix1[i][0], resultMatrix1[i][1]);
} //end for
//Solution 2 - strictly allocate matrix with size equal to the result.
//Without using dynamic allocation, meaning we need to have two passes.
//1st pass is to count the element which satisfy the criteria
int usedCount2=0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
usedCount2++;
} //end if
} //end for
} //end for
int resultMatrix2[usedCount2][2]; //allocate the right size
int idx=0;
//2nd pass is to fill in the result matrix
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
resultMatrix2[idx][0] = i;
resultMatrix2[idx][1] = j;
idx++;
} //end if
} //end for
} //end for
//Print the result
printf("\nSolution 2\n");
for (int i = 0; i < usedCount2; i++){
printf("%d %d\n", resultMatrix2[i][0], resultMatrix2[i][1]);
} //end for
return 0;
}
两种解决方案的结果相同:
Solution 1
0 0
0 2
1 1
1 2
2 2
Solution 2
0 0
0 2
1 1
1 2
2 2
我必须实现一个函数,给定一个 0 和 1 的矩阵,returns 一个包含 1 坐标的新矩阵。例如:如果矩阵是 3x3 并且输出是:
1 0 1
0 1 1
0 0 1
新矩阵为 5x2,输出为:
0 0
0 2
1 1
1 2
2 2
有什么建议吗?我的方法是这样的:
int matrix[3][3];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (matrix[i][i] == 1){
//Code i need
}
}
}
如果matrix[i][j] == 1
你对坐标[i,j]
了解多少?那他们的坐标是1
:)
其次,输入矩阵总是3x3吗?如果没有,您需要将矩阵的维度存储在变量中,并将这些变量用于 for
循环。
解决方案确实取决于要求:
- 是否允许分配尽可能大的结果矩阵(在本例中为 9 x 2)。
- 如果第1点不允许,是否严格要求使用固定大小的数组(无动态分配)。如果是这种情况,则可能需要将矩阵传递两次以分配正确大小的数组。
其他解决方案当然是使用动态分配(使用 malloc 等)。
选项1和2的简化版本如下所示:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int matrix[3][3];
matrix[0][0] = 1;
matrix[0][1] = 0;
matrix[0][2] = 1;
matrix[1][0] = 0;
matrix[1][1] = 1;
matrix[1][2] = 1;
matrix[2][0] = 0;
matrix[2][1] = 0;
matrix[2][2] = 1;
//Solution 1 - If allowed to allocate matrix with size more than the result,
//i.e. if input is 3x3 matrix, then the maximum size of result matrix is 9 x 2
int resultMatrix1[9][2];
int usedCount1=0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
resultMatrix1[usedCount1][0] = i;
resultMatrix1[usedCount1][1] = j;
usedCount1++;
} //end if
} //end for
} //end for
//Print the result
printf("\nSolution 1\n");
for (int i = 0; i < usedCount1; i++){
printf("%d %d\n", resultMatrix1[i][0], resultMatrix1[i][1]);
} //end for
//Solution 2 - strictly allocate matrix with size equal to the result.
//Without using dynamic allocation, meaning we need to have two passes.
//1st pass is to count the element which satisfy the criteria
int usedCount2=0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
usedCount2++;
} //end if
} //end for
} //end for
int resultMatrix2[usedCount2][2]; //allocate the right size
int idx=0;
//2nd pass is to fill in the result matrix
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
resultMatrix2[idx][0] = i;
resultMatrix2[idx][1] = j;
idx++;
} //end if
} //end for
} //end for
//Print the result
printf("\nSolution 2\n");
for (int i = 0; i < usedCount2; i++){
printf("%d %d\n", resultMatrix2[i][0], resultMatrix2[i][1]);
} //end for
return 0;
}
两种解决方案的结果相同:
Solution 1
0 0
0 2
1 1
1 2
2 2
Solution 2
0 0
0 2
1 1
1 2
2 2