gpt4 book ai didi

c - 打印二维字符数组的镜像

转载 作者:行者123 更新时间:2023-11-30 20:38:04 25 4
gpt4 key购买 nike

请检查此代码的问题。
当我在 mycodeschool IDE 上运行它时,它显示编译错误。问题陈述是问题陈述给定一个二维数组,如果镜子沿着数组的一侧放置,则打印其镜像。

输入输入的第一行将包含一个数字 T = 测试用例的数量。每个测试用例将在一行中包含两个正整数 n 和 m (1<=n, m<=50),并用空格分隔。接下来的 n 行每行将包含一个正好包含 m 个字符的字符串。下一行将包含字符“V”或“H”。如果字符是V,则镜子沿着最右边的列垂直放置。如果字符为 H,则镜子沿最底行水平放置。

输出对于每个测试用例,打印 n*m 镜像 - n 行,每行包含 m 个字符的字符串。在每个测试用例的输出后打印一个额外的空行。

示例输入23 3ABC定义吉V3 4123456789876H样本输出篮球协会美联储

987656781234

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

int main()
{
char a[10][10];
int i,j,t,m,n;
char s;
scanf("%d\n",&t);
for(j=0;j<t;j++)
{
scanf("%d%d\n",&m,&n);

for(i=0;i<m;i++)
{
scanf("%s",&a[i]);
}

scanf("\n%c",&s);
if(s=='V')//for image along rightmost vertical line
{
for(i=0;i<m;i++)
{
strrev(a[i]);
}

}
else if(s=='H')//for image along lowermost horizontal line
{
int t=0;
int b=m-1;
while(t<b)
{
char *temp = (char *)malloc((strlen(a[t]) + 1) * sizeof(char));//temporary variable to swap
strcpy(temp, a[t]);
strcpy(a[t], a[b]);
strcpy(a[b], temp);
free(temp);
t++;
b--;
}
}

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

最佳答案

我认为输入字符数组 n 行恰好有 m 个字符存在问题......这是我的代码........

int main()
{
int tt,i,j,r,c,t,ch;
char a[20][20];
scanf("%d",&t);

for(tt=1;tt<=t;t++)
{
scanf("%d\t%d",&r,&c);

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%c",&a[i][j]);
}
printf("\n");
}
scanf("%c",&ch);

if(ch=='V')
{
for(i=0;i<r;i++)
{
for(j=(c-1);j>=0;j--)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
}
else if(ch=='H')
{
for(i=(r-1);i>=0;i--)
{
for(j=0;j<c;j++)
{
printf("%d",a[i][j]);
printf("\t");
}
printf("\n");
}
}
else
printf("error");
}
printf("");
getchar();
return 0;
}

关于c - 打印二维字符数组的镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30677137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com