为什么此代码适用于 1 和 2 但对于大于 3 的输入却失败?
Why this code is working for 1 and 2 yet fails for input that is more than 3?
所以我想在 C 中打印模式。
For n = 2
Output:
2 2 2
2 1 2
2 2 2
for n = 3
Output:
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
and so on.
我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int nn = n;
int *arr;
arr = (int*)malloc(n*sizeof(int));
int f = 0; //flag to check if I reached the mid of the pattern
int l = 2*n-1; //Lenght of square to be generated
int temp1 = 0;
int temp2 = l;
for(int i = 0;i<l;i++)
{
for(int j = temp1;j<temp2;j++) //change values in range temp1 to temp2
{
arr[j] = n;
}
for(int k = 0;k<l;k++)
{
printf("%d ",arr[k]);
}
printf("\n");
if(n == 1)
{
f = 1;
}
if(f==0) //For upper half of pattern
{
n=n-1;
temp1=temp1+1;
temp2=temp2-1;
}
else if(f==1) //For lower half of pattern
{
n=n+1;
temp1=temp1-1;
temp2=temp2+1;
}
}
return(0);
}
我得到了 n = 2 的正确输出,但是当我输入大于 2 的任何内容时,代码崩溃了。
我找不到应该做什么。谁能帮帮我,告诉我我做错了什么?
在这段代码中
for(int j = temp1;j<temp2;j++) //change values in range temp1 to temp2
{
arr[j] = n;
}
j 可以大于 n。但是 arr malloc space 是 n。数组溢出。
稍作改动即可使用
arr = (int*)malloc(2*n*sizeof(int));
最好按轴检查距离并取最大值打印。
类似于:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int vertical_distance;
int horizontal_distance;
int n;
scanf("%d",&n);
for(int i = 0; i < n * 2 + 1; i++)
{
for(int j = 0; j < n * 2 + 1; j++)
{
vertical_distance = abs(n - i);
horizontal_distance = abs(n - j);
if (vertical_distance > horizontal_distance)
printf("%d ", vertical_distance);
else
printf("%d ", horizontal_distance);
}
printf("\n");
}
return(0);
}
此外,当我 运行 您的代码时,它可以很好地处理大数字(3、7、15)。
我刚刚将您的代码粘贴到 onlinegdb.com/online_c_compiler 和 运行 中。能否请您添加您收到的错误消息?
(对不起我的英文)
所以我想在 C 中打印模式。
For n = 2
Output:
2 2 2
2 1 2
2 2 2
for n = 3
Output:
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
and so on.
我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
int nn = n;
int *arr;
arr = (int*)malloc(n*sizeof(int));
int f = 0; //flag to check if I reached the mid of the pattern
int l = 2*n-1; //Lenght of square to be generated
int temp1 = 0;
int temp2 = l;
for(int i = 0;i<l;i++)
{
for(int j = temp1;j<temp2;j++) //change values in range temp1 to temp2
{
arr[j] = n;
}
for(int k = 0;k<l;k++)
{
printf("%d ",arr[k]);
}
printf("\n");
if(n == 1)
{
f = 1;
}
if(f==0) //For upper half of pattern
{
n=n-1;
temp1=temp1+1;
temp2=temp2-1;
}
else if(f==1) //For lower half of pattern
{
n=n+1;
temp1=temp1-1;
temp2=temp2+1;
}
}
return(0);
}
我得到了 n = 2 的正确输出,但是当我输入大于 2 的任何内容时,代码崩溃了。 我找不到应该做什么。谁能帮帮我,告诉我我做错了什么?
在这段代码中
for(int j = temp1;j<temp2;j++) //change values in range temp1 to temp2
{
arr[j] = n;
}
j 可以大于 n。但是 arr malloc space 是 n。数组溢出。
稍作改动即可使用
arr = (int*)malloc(2*n*sizeof(int));
最好按轴检查距离并取最大值打印。
类似于:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int vertical_distance;
int horizontal_distance;
int n;
scanf("%d",&n);
for(int i = 0; i < n * 2 + 1; i++)
{
for(int j = 0; j < n * 2 + 1; j++)
{
vertical_distance = abs(n - i);
horizontal_distance = abs(n - j);
if (vertical_distance > horizontal_distance)
printf("%d ", vertical_distance);
else
printf("%d ", horizontal_distance);
}
printf("\n");
}
return(0);
}
此外,当我 运行 您的代码时,它可以很好地处理大数字(3、7、15)。 我刚刚将您的代码粘贴到 onlinegdb.com/online_c_compiler 和 运行 中。能否请您添加您收到的错误消息?
(对不起我的英文)