题解 第二章暴力求解|#叠筐#
把一个个大小差一圈的筐叠上去,使得从上往下看,边框花色交错
输入:三元组,外筐尺寸(必须是奇数),中心字符,边筐字符,均为ASCII
输出:叠在一起的筐筐图案,边筐角被打磨掉
例如输入:
11 B A
5 @ W
得到:
11 B A
BBBBBBBBB
BAAAAAAAAAB
BABBBBBBBAB
BABAAAAABAB
BABABBBABAB
BABABABABAB
BABABBBABAB
BABAAAAABAB
BABBBBBBBAB
BAAAAAAAAAB
BBBBBBBBB
5 @ W
@@@
@WWW@
@W@W@
@WWW@
@@@
代码如下:
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
void BasketWeaving(char (*Pres)[80],
int Num,
char CenterChar,
char OtherChar)
{
//对于每一个筐
//边长分别为Num,Num-2,Num-4...1
//其中Num必须为奇数
//边筐的中心点坐标为Num/2
//边筐的层数为0-Num/2(最外层为0层)
//边筐的左上坐标为(i,i),右下为(Num-1-i)
int Layer = Num / 2;
//逐层打印
for (int i = 0; i <= Layer; i++)
{
//确定当前层的符号
char PresentChar;
if (i % 2 == 0)
PresentChar = CenterChar;
else
PresentChar = OtherChar;
//第一个for循环打印顶层
for (int top = i; top <= Num - 1 - i; top++)
Pres[i][top] = PresentChar;
//第二个for循环打印中间层
for (int middle = i + 1; middle <= Num - 2 - i; middle++)
{
Pres[middle][i] = PresentChar;
Pres[middle][Num - 1 - i] = PresentChar;
}
//第三个for循环打印底层
for (int bottom = i; bottom <= Num - 1 - i; bottom++)
Pres[Num - 1 - i][bottom] = PresentChar;
}
//将最外层边角磨平
Pres[0][0] = ' ';
Pres[Num - 1][Num - 1] = ' ';
Pres[0][Num - 1] = ' ';
Pres[Num - 1][0] = ' ';
}
int main()
{
char Pres[80][80] = {0};
int Num;
char CenterChar, OtherChar;
while (cin >> Num >> CenterChar >> OtherChar)
{
BasketWeaving(Pres,
Num,
CenterChar,
OtherChar);
for (int i = 0; i < Num; i++)
{
for (int j = 0; j < Num; j++)
cout << Pres[i][j];
cout << '\n';
}
}
return 0;
}
/*
5 s g
sss
sgggs
sgsgs
sgggs
sss
*/
/*
13 % #
%%%%%%%%%%%
%###########%
%#%%%%%%%%%#%
%#%#######%#%
%#%#%%%%%#%#%
%#%#%###%#%#%
%#%#%#%#%#%#%
%#%#%###%#%#%
%#%#%%%%%#%#%
%#%#######%#%
%#%%%%%%%%%#%
%###########%
%%%%%%%%%%%
*/
王道机试指南刷题 文章被收录于专栏
计划刷完这本书