gpt4 book ai didi

c - 在数组中打印 'box'

转载 作者:行者123 更新时间:2023-11-30 17:45:38 24 4
gpt4 key购买 nike

由于我第一年的任务需要这个子任务,所以我每天都面临着脱发的问题。

我需要在初始化 板中心处的数字周围打印一个“” strong> 程序的阶段。

“盒子”由覆盖两侧的“|”和位于坐标 [5][5 处数字顶部和底部的“_”组成]

当我执行此程序时,面板显示,但“”不显示。 为什么会发生这种情况?

这是代码:

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

//FUNCTION: Draw the Board
int drawBoard()
{
//Declare array size
int board[9][9];

//initialize variables
int rows, columns, randomNumber, flag;

//random number seed generator
srand(time(NULL));

for ( rows = 0 ; rows < 9 ; rows++ )
{

for ( columns = 0 ; columns < 9 ; columns++ )
{
flag = 0;

do
{
//generate random numbers from 2 - 8

randomNumber = rand() %7 + 2;

board[rows][columns] = randomNumber;

//Display the 'box' if rows and columns == 5 / i.e - board[5][5]
if ( rows == 5 && columns == 5 )
{ //Checks for 2 adjacent numbers
if ( board[rows][columns] == board[rows - 1][columns] || board[rows][columns] == board[rows][columns - 1] )
{
flag = 0;
continue;
}
else
{
flag = 1;

//Print 'box'
marker( rows, columns );
}
}

//Checks for 2 adjacent numbers.
if ( board[rows][columns] == board[rows - 1][columns] || board[rows][columns] == board[rows][columns - 1] )
{
flag = 0;
continue;
}

else
//Prints the correct board
{
flag = 1;
printf( " %d ", board[rows][columns] );
}

} while ( flag == 0 ); //end outer do-while

}//end inner for-loop

printf("\n\n");

}//end outer for-loop

}//end FUNCTION drawBoard

//FUNCTION: Mark the surrounding of the number with "|" and "_" at board[5][5]
void marker( int x, int y, int** board )
{
board[x][y-1] == "\n _ ";
board[x][y+1] == "\n _ ";
board[x-1][y] == " |";
board[x+1][y] == "| ";
}

int main()
{
drawBoard();
}

TI

最佳答案

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

//FUNCTION: Draw the Board
char tochar(int i)
{
return i+'0';
}
void marker( int x, int y, char board[][19] );
int check(int x, int y, char board[][19])
{
if(x>1&&board[2*x+1][2*y+1]==board[2*x-1][2*y+1])
return 1;

if(y>1&&board[2*x+1][2*y+1]==board[2*x+1][2*y-1])
return 1;
return 0;
}
int drawBoard()
{
//Declare array size
char board[19][19];

memset(board,32, sizeof(board));
//initialize variables
int rows, columns, randomNumber, flag;

//random number seed generator
srand(time(NULL));

for ( rows = 0 ; rows < 9 ; rows++ )
{

for ( columns = 0 ; columns < 9 ; columns++ )
{
flag = 0;

do
{
//generate random numbers from 2 - 8

randomNumber = rand() %7 + 2;
board[2*rows+1][2*columns+1] = tochar(randomNumber);

//Display the 'box' if rows and columns == 5 / i.e - board[5][5]
if ( rows == 4 && columns == 4 )
{ //Checks for 2 adjacent numbers
if ( check(rows, columns, board))
{
flag = 0;
}
else
{
flag = 1;

marker( 2*rows+1, 2*columns+1, board );
//Print 'box'
}
}
else
{
//Checks for 2 adjacent numbers.
if ( check(rows, columns, board))
{
flag = 0;
}

else
//Prints the correct board
{
flag = 1;
// printf( " %c ", board[2*rows+1][2*columns+1] );
}
}
} while ( flag == 0 ); //end outer do-while

}//end inner for-loop

printf("\n\n");

}//end outer for-loop
for(rows=0;rows<19;rows++)
{
for(columns=0;columns<19;columns++)
{
printf("%c",board[rows][columns]);
}
printf("\n");
}
}//end FUNCTION drawBoard

//FUNCTION: Mark the surrounding of the number with "|" and "_" at board[5][5]
void marker( int x, int y, char board[][19] )
{
board[x][y-1] = '|';
board[x][y+1] = '|';
board[x-1][y] = '_';
board[x+1][y] = '_';
/*
192
191
217
218
*/
}

int main()
{
drawBoard();
}

关于c - 在数组中打印 'box',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19569677/

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