问题描述如下:我的输入矩阵不包含零,但在打印矩阵时却出现了零。

huangapple go评论73阅读模式
英文:

My input matrix does not contain zeros but i get zeros when I print the matrix.The problem description is given below

问题

Matrix Layer Rotation (hacker rank problem)

给定一个尺寸为 m x n 的二维矩阵和一个正整数 r,你需要旋转这个矩阵 r 次并打印结果矩阵。旋转方向应为逆时针方向。

一个 4 x 5 矩阵的旋转如下图所示。请注意,在每次旋转中,你只需将元素向左移动一步。

保证 m 和 n 中的较小值为偶数。

例如,将 Start 矩阵旋转 2 次:

Start         First           Second
 1 2 3 4        2  3  4  5      3  4  5  6
12 1 2 5  ->   1  2  3  6 ->   2  3  4  7
11 4 3 6      12  1  4  7       1  2  1  8
10 9 8 7      11 10  9  8     12 11 10  9
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int d=0,m,n,r;
    long i,j;
    scanf("%d %d %d",&m,&n,&r);
    long **a;
    a=(long**)malloc(m*sizeof(long*));
    for(i=0;i<m;i++)
    {
        a[i]=(long*)malloc(n*sizeof(long));
    }
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%ld",&a[i][j]);
        }
    }
    
    while(d<r)
    {
        int lr=m-1,lc=n-1,ro=0,c=0;
        long **b;
        b=(long**)malloc(m*sizeof(long*));
        for(i=0;i<m;i++)
        {
            b[i]=(long*)malloc(n*sizeof(long));
        }
        while(ro<lr && c<lc)
        {
            if(c<lc)
            {
                for(i=lc;i>=c;i--)
                {
                  if(i==lc)
                  {
                    b[ro][i]=a[ro+1][lc];
                  }
                  else
                  {
                    b[ro][i]=a[ro][i+1];
                  }
                }
              ro++;

            }
          
          for(i=ro;i<=lr;i++)
          {
            b[i][c]=a[i-1][c];
          }
          c++;
          for(i=c;i<=lc;i++)
          {
            b[lr][i]=a[lr][i-1];
          }
          lr--;
          if(ro<lr)
          {
            for(i=lr;i>=ro;i--)
            {
              b[i][lc]=a[i+1][lc];
            }
          lc--;

          }
          
        }
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                a[i][j]=b[i][j];
            }
        }
        d++;
    }
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%ld ",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

编译器消息
答案错误

输入(stdin)
5 4 7
1 2 3 4
7 8 9 10
13 14 15 16
19 20 21 22
25 26 27 28

你的输出(stdout)
0 0 0 0
0 0 0 0
0 0 0 0 (0 是问题)
10 0 0 0
4 3 2 1

预期输出
28 27 26 25
22 9 15 19
16 8 21 13
10 14 20 7
4 3 2 1

测试案例 2
答案错误

输入(stdin)
5 4 7
1 2 3 4
7 8 9 10
13 14 15 16
19 20 21 22
25 26 27 28

你的输出(stdout)
28 27 26 25
22 0 0 19
16 0 0 13
10 0 0 7
4 3 2 1

预期输出
28 27 26 25
22 9 15 19
16 8 21 13
10 14 20 7
4 3 2 1

英文:

Matrix Layer Rotation(hacker rank problem)

You are given a 2D matrix of dimension m X n and a positive integer r. You have to rotate the matrix times and print the resultant matrix. Rotation should be in the anti-clockwise direction.

The rotation of a 4 X 5 matrix is represented by the following figure. Note that in one rotation, you have to shift elements by one step only.

matrix-rotation

It is guaranteed that the minimum of m and n will be even.

As an example rotate the Start matrix by 2:

Start         First           Second
1 2 3 4        2  3  4  5      3  4  5  6
12 1 2 5  -&gt;   1  2  3  6 -&gt;   2  3  4  7
11 4 3 6      12  1  4  7       1  2  1  8
10 9 8 7      11 10  9  8     12 11 10  9
#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
int main()
{
int d=0,m,n,r;
long i,j;
scanf(&quot;%d %d %d&quot;,&amp;m,&amp;n,&amp;r);
long **a;
a=(long**)malloc(m*sizeof(long*));
for(i=0;i&lt;m;i++)
{
a[i]=(long*)malloc(n*sizeof(long));
}
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;n;j++)
{
scanf(&quot;%ld&quot;,&amp;a[i][j]);
}
}
while(d&lt;r)
{
int lr=m-1,lc=n-1,ro=0,c=0;
long **b;
b=(long**)malloc(m*sizeof(long*));
for(i=0;i&lt;m;i++)
{
b[i]=(long*)malloc(n*sizeof(long));
}
while(ro&lt;lr &amp;&amp; c&lt;lc)
{
if(c&lt;lc)
{
for(i=lc;i&gt;=c;i--)
{
if(i==lc)
{
b[ro][i]=a[ro+1][lc];
}
else
{
b[ro][i]=a[ro][i+1];
}
}
ro++;
}
for(i=ro;i&lt;=lr;i++)
{
b[i][c]=a[i-1][c];
}
c++;
for(i=c;i&lt;=lc;i++)
{
b[lr][i]=a[lr][i-1];
}
lr--;
if(ro&lt;lr)
{
for(i=lr;i&gt;=r;i--)
{
b[i][lc]=a[i+1][lc];
}
lc--;
}
}
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;n;j++)
{
a[i][j]=b[i][j];
}
}
d++;
}
for(i=0;i&lt;m;i++)
{
for(j=0;j&lt;n;j++)
{
printf(&quot;%ld &quot;,a[i][j]);
}
printf(&quot;\n&quot;);
}
return 0;
}
Compiler Message
Wrong Answer
Input (stdin)
5 4 7
1 2 3 4
7 8 9 10
13 14 15 16
19 20 21 22
25 26 27 28
Your Output (stdout)
0 0 0 0 
0 0 0 0 
0 0 0 0    (0 is the problem)
10 0 0 0 
4 3 2 1 
Expected Output
28 27 26 25
22 9 15 19
16 8 21 13
10 14 20 7
4 3 2 1
test case 2
Wrong Answer
Input (stdin)
5 4 7
1 2 3 4
7 8 9 10
13 14 15 16
19 20 21 22
25 26 27 28
Your Output (stdout)
28 27 26 25 
22 0 0 19 
16 0 0 13 
10 0 0 7 
4 3 2 1 
Expected Output
28 27 26 25
22 9 15 19
16 8 21 13
10 14 20 7
4 3 2 1

问题描述如下:我的输入矩阵不包含零,但在打印矩阵时却出现了零。

答案1

得分: 0

以下是您提供的代码的翻译部分:

在您的 while(d&lt;r) 循环内部,每次执行都应该完成 a 的单个位置旋转。while(d&lt;r) { ... d++; } 然后导致循环体执行 r 次。

您可能在这个循环中遇到问题:

for(i=lr;i&gt;=r;i--) {
    b[i][lc]=a[i+1][lc];
}

其中 i&gt;=r。循环体中的任何内容都不应该与 dr 有关。单个位置旋转不应该取决于将执行多少次单个旋转(或已经执行了多少次)。明白吗?

--- 添加我自己的实现,根据我理解您的要求 ---

以下似乎可以满足您的需求。无论如何,我已经加了注释,以使每个操作的目的更加清晰。如果对您有用,请随意使用。

#include <stdio.h>
#include <stdlib.h>

int main() {
  int m, n, repetitions;
  long i, j;
  scanf("%d %d %d", &m, &n, &repetitions);
  long **a;
  a = (long **)malloc(m * sizeof(long *));
  for (i = 0; i < m; i++) {
    a[i] = (long *)malloc(n * sizeof(long));
  }
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      scanf("%ld", &a[i][j]);
    }
  }

  long **b;
  b = (long **)malloc(m * sizeof(long *));
  for (i = 0; i < m; i++) {
    b[i] = (long *)malloc(n * sizeof(long));
  }

  while (repetitions--) {
    // 左上角、下行、左列
    int ul = 0, lr = m - 1, lc = n - 1;

    // 执行单个位置旋转,从外部环向内部工作。当环的宽度或高度为1时停止。
    while (ul < lr && ul < lc) {
      // 将左边缘向下移动
      for (int rr = ul; rr < lr; ++rr) b[rr + 1][ul] = a[rr][ul];

      // 将底边向右移动
      for (int cc = ul; cc < lc; ++cc) b[lr][cc + 1] = a[lr][cc];

      // 将右边缘向上移动
      for (int rr = lr; rr > ul; --rr) b[rr - 1][lc] = a[rr][lc];

      // 将顶边向左移动
      for (int cc = lc; cc > ul; --cc) b[ul][cc - 1] = a[ul][cc];

      // 将环的左上角向右下移动1。
      ++ul;
      // 将环的底边向上移动1。
      --lr;
      // 将环的右边向左移动1。
      --lc;
    }

    for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
        a[i][j] = b[i][j];
      }
    }
  }
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      printf("%ld ", a[i][j]);
    }
    printf("\n");
  }
  return 0;
}

如果您有其他问题或需要进一步的帮助,请告诉我。

英文:

The code within your while(d&lt;r) loop should accomplish a single position rotation of a each time it is executed. while(d&lt;r) { ... d++; } then causes the body of the loop to be executed r times.

The issue that you're having likely arises on this loop:

for(i=lr;i&gt;=r;i--) {
b[i][lc]=a[i+1][lc];
}

with i&gt;=r. Nothing in the body of the loop should be a function of either d or r. A single position rotation should not depend on how many single rotations will be performed (or have already been performed). Clear?

--- Adding my own implementation of what I think you're asking for ---

The following seems to do what you need. In any case, I've commented it to make the purpose of each operation more clear. Feel free to use it if it works for you.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
int main()
{
int m, n, repetitions;
long i, j;
scanf(&quot;%d %d %d&quot;, &amp;m, &amp;n, &amp;repetitions);
long **a;
a = (long **)malloc(m * sizeof(long *));
for (i = 0; i &lt; m; i++) {
a[i] = (long *)malloc(n * sizeof(long));
}
for (i = 0; i &lt; m; i++) {
for (j = 0; j &lt; n; j++) {
scanf(&quot;%ld&quot;, &amp;a[i][j]);
}
}
long **b;
b = (long **)malloc(m * sizeof(long *));
for (i = 0; i &lt; m; i++) {
b[i] = (long *)malloc(n * sizeof(long));
}
while (repetitions--) {
// Upper-left, lower-row, lower-column
int ul = 0, lr = m - 1, lc = n - 1;
// Perform a single position rotation, from the outer ring working in. Stop
// when either the width or height of the ring is 1.
while (ul &lt; lr &amp;&amp; ul &lt; lc) {
// Shift left-edge downwards
for (int rr = ul; rr &lt; lr; ++rr) b[rr + 1][ul] = a[rr][ul];
// Shift bottom-edge rightwards
for (int cc = ul; cc &lt; lc; ++cc) b[lr][cc + 1] = a[lr][cc];
// Shift right-edge upwards
for (int rr = lr; rr &gt; ul; --rr) b[rr - 1][lc] = a[rr][lc];
// Shift top-edge leftwards
for (int cc = lc; cc &gt; ul; --cc) b[ul][cc - 1] = a[ul][cc];
// Shift upper-left corner of ring to the right and down by 1.
++ul;
// Shift bottom-edge of ring up by 1.
--lr;
// Shift right-edge of ring to the left by 1.
--lc;
}
for (i = 0; i &lt; m; i++) {
for (j = 0; j &lt; n; j++) {
a[i][j] = b[i][j];
}
}
}
for (i = 0; i &lt; m; i++) {
for (j = 0; j &lt; n; j++) {
printf(&quot;%ld &quot;, a[i][j]);
}
printf(&quot;\n&quot;);
}
return 0;
}

huangapple
  • 本文由 发表于 2020年1月6日 22:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定