gpt4 book ai didi

C: 二维字符数组问题

转载 作者:太空宇宙 更新时间:2023-11-04 03:22:41 25 4
gpt4 key购买 nike

我的练习有问题。目的是打印一个int输入后的特定图片(假设输入>3)。

示例:

/image/KRl0R.png

这是我的代码:

#include <stdio.h>

int main(void){

int i, j, n, simbolo;
printf("Inserire un numero: ");
scanf("%d", &n);

char mat[n + 1][n + 2];
simbolo = n+2;

//initialization with blank space and setting 3 * diagonal
for(i = 0; i < n + 1; i ++){
for(j = 0; j < n + 2; j++){
mat[i][j] = ' ';
mat[n - 2][0] = '*';
mat[n - 1][1] = '*';
mat[n][2] = '*';
}
}

//Add * diagonal of n length
for(i = 0; i < n + 1; i++){
mat[i][simbolo] = '*';
for(int x = i, y = 0; y < n + 2; y++){ //Print current line
printf("%c", mat[x][y]);
}
printf("\n");
simbolo--;
}

return 0;

}

输出不正确,它在 mat[1][0] 中添加了一个额外的 '*':

/image/4Z5Fl.png

在此先感谢您的帮助

最佳答案

根据图像,您希望输出有 n 行,而不是 n+1。这可以正常工作:

#include <stdio.h>

int main(void){

int i, j, n, simbolo;
printf("Inserire un numero: ");
scanf("%d", &n);

char mat[n][n + 2];
simbolo = n+1;

//initialization with blank space and setting 3 * diagonal
for(i = 0; i < n; i ++){
for(j = 0; j < n + 2; j++){
mat[i][j] = ' ';
}
}

mat[n - 3][0] = '*';
mat[n - 2][1] = '*';
mat[n - 1][2] = '*';

//Add * diagonal of n length
for(i = 0; i < n; i++){
if(i<(n-1))
mat[i][simbolo] = '*';
printf("%.*s\n", n+2, mat[i]);
simbolo--;
}

return 0;
}

关于C: 二维字符数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43969126/

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