-->
当前位置:首页 > 题库 > 正文内容

7-7 p/n棋盘 (10 分)

Luz3年前 (2021-03-20)题库1678
7-7 p/n棋盘 (10 分)

输出字符'p'和'n'的棋盘。

输入格式:

一个数n(表示棋盘大小,即为2n2n的字符棋盘,其中n<10)。

输出格式:

输出一个2n2n的字符棋盘('字符由‘p’和‘n’组成,'p':为正,'n’为负的意思),其规则为:规模为n的棋盘的左上角,右上角和右下角为规模为n-1的棋盘,其左下角为左上角的取反(即对应字母为‘p’则变为'n';字母为‘n’则变为'p')。

输入样例:

1
2

输出样例:

pp
np
pppp
npnp
nnpp
pnnp
作者
严华云
单位
湖州师范学院
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include<bits/stdc++.h>
using namespace std;

int board[1000][1000];
void chess(int x,int y,int z,int size);


int main(int argc,const char *argv[])
{
	int n;
	int size;
	int i,j;
	
	//cout<<"输入2^n * 2^n的棋盘参数n:";
	cin>>n;
	
	size=pow(2,n);
	
	chess(1,1,1,size);
	
	for(i=1;i<=pow(2,n);i++)
	{
		for(j=1;j<=pow(2,n);j++)
		{
			if(board[i][j]==1)
			{
				cout<<"p";
			 } 
			else if(board[i][j]==0)
			{
				cout<<"n";
			}
		}
		cout<<endl;
	 } 
	 
	 return 0;
}

void chess(int x,int y,int z,int size)
{
	int half;
	
	if(size==1)
	{
		board[x][y]=z;
		return;
	}
	half=size/2;
	
	chess(x,y,z,half);
	chess(x,y+half,z,half);
	if(z==1){
		chess(x+half,y,0,half);
	}
	else{
		chess(x+half,y,1,half);
	}
	chess(x+half,y+half,z,half);
}


评论列表

访客
访客
3年前 (2021-03-25)

牛逼!

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。