排版,一种是可以直接输出,也可以先保存起来,再输出。找规律!!!
附
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
char buf[81][81];
int main(){
int n;
char in,out;
bool firstTest=true;
while(cin>>n>>in>>out){
if(firstTest==false)
cout<<endl;
else
firstTest=false;
int l=1;
char tmp=out;
for(int i=(n+1)/2;i>=1;i--){
if(tmp==out)
tmp=in;
else
tmp=out;
for(int j=i;j<i+l;j++)
{
buf[i][j]=tmp;
buf[j][i]=tmp;
}
for(int j=n+1-i;j>n+1-i-l;j--)
{
buf[n+1-i][j]=tmp;
buf[j][n+1-i]=tmp;
}
l=l+2;
}
if(n!=1)
{
buf[1][1]=' ';
buf[1][n]=' ';
buf[n][1]=' ';
buf[n][n]=' ';
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<buf[i][j];
cout<<endl;
}
}
return 0;
}
这道题是例题
接下来是Repeater
题目如下
-
Harmony is indispensible in our daily life and no one can live without it----may be Facer is the only exception. One day it is rumored that repeat painting will create harmony and then hundreds of people started their endless drawing. Their paintings were based on a small template and a simple method of duplicating. Though Facer can easily imagine the style of the whole picture, but he cannot find the essential harmony. Now you need to help Facer by showing the picture on computer.
You will be given a template containing only one kind of character and spaces, and the template shows how the endless picture is created----use the characters as basic elements and put them in the right position to form a bigger template, and then repeat and repeat doing that. Here is an example.# #
# <-template
# #
So the Level 1 picture will be# #
#
# #
Level 2 picture will be# # # #
# #
# # # #
# #
#
# #
# # # #
# #
# # # #
-
输入:
-
The input contains multiple test cases.
The first line of each case is an integer N, representing the size of the template is N*N (N could only be 3, 4 or 5).
Next N lines describe the template.
The following line contains an integer Q, which is the Scale Level of the picture.
Input is ended with a case of N=0.
It is guaranteed that the size of one picture will not exceed 3000*3000.
-
输出:
-
For each test case, just print the Level Q picture by using the given template.
-
样例输入:
-
3 # # # # # 1 3 # # # # # 3 4 OO O O O O OO 2 0
-
样例输出:
-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # OO OO O OO O O OO O OO OO OO OO O O O O O O O O OO OO OO OO O O O O O O O O OO OO OO OO O OO O O OO O OO OO
1.代码为了省内存,一共定义了三个二维数组,一个存储模板,另外两个缓冲区呢,是用来轮回使用的,意思就是层数分奇偶讨论,迭代
2.我看着大神的代码写,都写了好久,后来才发现自己在一个地方出错了,那就是cin不读入空格啊!!于是用的cin.getline(tmp[i],'\n'),还有一种写法是cin.getline(tmp[i],n+1)
第一个是这一行以回车换行结尾,第二个是表示这一行共输入n+1个字符,为什么不是n个字符呢?因为你最后还要回车换行啊!!!
还有一个注意的点就是,cin>>n后还有加一个cin.get(),因为你要是不加那你输入n后的回车就会被cin.getline输入进去了!!!
3.总之呢,还是最好在用数组前清空一下吧,就是置为空格
代码:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
char tmp[3001][3001];
char buf1[3001][3001];
char buf2[3001][3001];
void tempcopy(char source[3001][3001],char target[3001][3001],int x,int y,int n){
//x 和 y为首坐标的前一个坐标 n为source数组的维度
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
target[i+x][j+y]=source[i][j];
}
void nextlevel(char last[3001][3001],char cur[3001][3001],int level,int c,int n){
//level为层数,c为输入的特殊字符,n为维度
int max=pow((double)n,(double)level);
for(int i=0;i<max;i++)
for(int j=0;j<max;j++)
{//清空当前的数组
cur[i][j]=' ';
}
int offset=pow((double)n,(double)level-1);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(tmp[i][j]==c)
tempcopy(last,cur,i*offset,j*offset,offset);
}
int main(){
int n,q;
char c;
while(cin>>n&&n!=0){
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
tmp[i][j]=' ';
cin.get();
for(int i=0;i<n;i++)
cin.getline(tmp[i],'\n');
cin>>q;
int max=pow((double)n,(double)q);
for(int i=0;i<max;i++)
for(int j=0;j<max;j++)
{//清空
buf1[i][j]=' ';
buf2[i][j]=' ';
}
//第一次扩展
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{ buf1[i][j]=tmp[i][j];
if(tmp[i][j]!=' ')
c=tmp[i][j];//确定特殊字符
}
//剩下的几层扩展
for(int i=2;i<=q;i++)
{
if(i%2!=0)//奇数层
nextlevel(buf2,buf1,i,c,n);
else
nextlevel(buf1,buf2,i,c,n);
}
if(q%2!=0)
for(int i=0;i<max;i++){
for(int j=0;j<max;j++)
cout<<buf1[i][j];
cout<<endl;
}
else
for(int i=0;i<max;i++){
for(int j=0;j<max;j++)
cout<<buf2[i][j];
cout<<endl;
}
}
return 0;
}