我在第一行有 3 个整数,前两个是矩阵的 N 行和 M 行和列,X 表示我必须放大矩阵的次数。 在接下来的 N 行中,我有 M 个元素,更确切地说是一个矩阵,我必须“放大”。
这项工作是用四个 for
语句完成的,但我不知道该怎么做。
输入
2 2 3
1 2
3 4
输出
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
输出是给定的矩阵,其大小增加了 X。
我的代码
void ZoomIn(int n, int m, int x, int a[][100], int aux[][100])
{
int i, j, ind_i, I, J, ind_j;
if (x == 0)
return ;
I = J = 0;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
for(ind_i = J; ind_i < x; ind_i++)
for(ind_j = I; ind_j < x; ind_j++)
aux[ind_i][ind_j] = a[i][j]; // the first element in the smallest matrix
I = I + x;
}
}
如何正确增加 I
和 J
的值,以便创建更小的子矩阵?
我看到这个过程的方式是
我必须创建
1 1 1
1 1 1
1 1 1
然后继续下一个小矩阵
2 2 2
2 2 2
2 2 2
最佳答案
如果您可以使用 C99(或 C11)可变长度数组,这是迄今为止最简单的。只要确保数组对于堆栈来说不是太大,就可以在 main()
中直接分配局部变量,然后将数组传递给 ZoomIn()
进行处理。
您建议您应该使用四个嵌套循环。这当然有效。外部一对嵌套循环遍历基本(未缩放)数组中的元素;内部一对嵌套循环将基本数组的当前元素复制到缩放数组的相关子部分。
此代码使用在 stderr.h
中声明并在 stderr.c
中定义的函数 — 代码可从 https://github.com/jleffler/soq/tree/master/src/libsoq 获得.这些函数极大地简化了错误报告。
#include <stdio.h>
#include <string.h>
#include "stderr.h"
static void dump_array(const char *tag, int n, int m, int array[n][m])
{
printf("%s (%dx%d):\n", tag, n, m);
for (int i = 0; i < n; i++)
{
const char *pad = "";
for (int j = 0; j < m; j++)
{
printf("%s%d", pad, array[i][j]);
pad = " ";
}
putchar('\n');
}
}
static void ZoomIn(int r, int c, int base[r][c], int z, int zoom[z * r][z * c])
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
for (int k = z * i; k < z * (i + 1); k++)
{
for (int l = z * j; l < z * (j + 1); l++)
zoom[k][l] = base[i][j];
}
}
}
}
int main(int argc, char **argv)
{
err_setarg0(argv[0]);
if (argc > 1)
err_usage("");
char buffer[4096];
if (fgets(buffer, sizeof(buffer), stdin) == 0)
err_error("Unexpected EOF\n");
int r, c, z;
buffer[strcspn(buffer, "\n")] = '\0';
if (sscanf(buffer, "%d%d%d", &r, &c, &z) != 3)
err_error("Expected 3 numbers on first line, not [%s]\n", buffer);
if (r <= 0 || r > 100 || c <= 0 || c > 100 || z <= 0 || z > 100)
err_error("Matrix size out of control (r = %d, c = %d, z = %d)\n", r, c, z);
if (r * c * z * z > 1000000)
err_error("Zoomed matrix too big (r = %d, c = %d, z = %d)\n", r, c, z);
int base[r][c];
for (int i = 0; i < r; i++)
{
if (fgets(buffer, sizeof(buffer), stdin) == 0)
err_error("Unexpected EOF 2\n");
buffer[strcspn(buffer, "\n")] = '\0';
int offset = 0;
for (int j = 0; j < c; j++)
{
int p;
if (sscanf(buffer + offset, "%d%n", &base[i][j], &p) != 1)
err_error("Format error on line [%s]\n", buffer);
offset += p;
}
}
dump_array("Base Array", r, c, base);
int zoom[r*z][c*z];
ZoomIn(r, c, base, z, zoom);
dump_array("Zoomed Array", r * z, c * z, zoom);
return 0;
}
给定的数据文件:
2 2 3
1 2
3 4
输出是:
Base Array (2x2):
1 2
3 4
Zoomed Array (6x6):
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
给定的数据文件:
4 5 6
1 3 5 7 9
2 4 6 8 0
0 1 2 3 4
5 6 7 8 9
输出是:
Base Array (4x5):
1 3 5 7 9
2 4 6 8 0
0 1 2 3 4
5 6 7 8 9
Zoomed Array (24x30):
1 1 1 1 1 1 3 3 3 3 3 3 5 5 5 5 5 5 7 7 7 7 7 7 9 9 9 9 9 9
1 1 1 1 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6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 9 9 9 9 9 9
您还可以仅使用 2 个循环来编写缩放代码,但每次迭代需要进行两次除法运算:
static void ZoomIn(int r, int c, int base[r][c], int z, int zoom[z * r][z * c])
{
for (int k = 0; k < z * r; k++)
{
for (int l = 0; l < z * c; l++)
zoom[k][l] = base[k/z][l/z];
}
}
这产生与四重嵌套循环版本相同的输出。尽管代码更简洁,但尚不清楚它是否会比四重嵌套循环版本更快。
关于c - 增加矩阵的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44606569/