题目链接请点我
题解:
有几个比较坑的地方:
1.当num=1时;
2.两个输出数据之间空一行;
3.最里面一层一定是第一个字母,最外面一层不确定
接着找到规律,按行输出就好了。
这种卡输入输出的题目用文件读入会比较好。
代码实现:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define MAX 82
using namespace std;
int main(){
char A,B;
int num;
char save[MAX];
bool flag = false;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while( scanf("%d %c %c",&num,&A,&B)!=EOF ){
if( flag ){
printf("\n");
}
if( num == 1 ){
printf("%c\n",A);
flag = true;
continue;
}
if( ((num+1)/2+1)%2!=0 ){
char xx = A;
A = B;
B = xx;
}
for( int i = 1; i <= num; i++ ){
memset(save,0,sizeof(save));
int j;
char tmp;
int length = (num+1)/2-abs((num+1)/2-i);
if( i%2 == 0 ){
tmp = B;
}
else{
tmp = A;
}
for(j = 1; j <= length; j++ ){
if( j%2 == 1 ){
save[j] = A;
}
else{
save[j] = B;
}
}
for(j = length+1; j <= num-length; j++ ){
save[j] = tmp;
}
for(j = num-length+1; j <= num; j++ ){
if( j%2 == 1 ){
save[j] = A;
}
else{
save[j] = B;
}
}
if( i == 1 || i == num ){
save[1] = ' ';
save[num] = ' ';
}
for( j = 1; j <= num; j++ ){
printf("%c",save[j]);
}
printf("\n");
}
flag = true;
}
return 0;
}