【排版问题】九度OJ 1161: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     

二、代码及注释

#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
char temp[6][6];
char buf[3001][3001];
int n,q;
//level用于判断递归层数,x,y为起始坐标
void update(int level,int x,int y){
    //若层数为1,则直接重复原来的图像
    if(level==1){
        for(int i=x;i<x+n;i++){
            for(int j=y;j<y+n;j++){
                buf[i][j]=temp[i-x][j-y];
            }
        }
    }else{
        int n1=pow(n,level);
        int n2=pow(n,level-1);
        for(int i=x;i<x+n1;i+=n2){
            for(int j=y;j<y+n1;j+=n2){
                if(temp[(int)((i-x)/n2)][(int)((j-y)/n2)]!=' '){//如果原来图像不为空格,那么进行复制
                    update(level-1,i,j);
                }
            }
        }
    }
}
int main(){
    while(scanf("%d",&n)!=EOF){
        if(n==0) break;
        getchar();//这里注意,因为下边要使用gets,gets会把enter当做一行,所以这里先getchar一下
        for(int i=0;i<n;i++){
           gets(temp[i]);
        }
        scanf("%d",&q);
        int sqrt=pow(n,q);//这里先计算一下,否则下边报错
        for(int i=0;i<sqrt;i++){
            for(int j=0;j<sqrt;j++){
                buf[i][j]=' ';
            }
        }
        update(q,0,0);
        for(int i=0;i<sqrt;i++){
            for(int j=0;j<sqrt;j++){
                printf("%c",buf[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mad Idea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值