repeater(北京大学复试上机题)图形排版

网址:
http://t.cn/E9jcaVb

描述
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 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.
示例1

输入:
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     

【题目大意】给你一个仅包含一种字符和空格的模板,模板展示如何创建无尽的图片,将字符用作基本元素并将它们放在正确的位置以形成更大的模板,然后不断进行该操作。

【分析】
本题是个排版题,但无法按照从左到右从上到下的顺序进行输出,只能先排版再输出。
根据下面的图,我们可以想到,首先要求出新模板的左上角坐标,然后将旧模板从左上角位置开始摆放即可,那么左上角坐标如何求呢。
根据下面的图可以知道,新模板的左上角坐标是(0,0),(0,6),(3,3),(6,0),(6,6)五个坐标,他们分别是旧模板矩阵的每个点坐标乘以3得到的。
而更大一个级别的图片的左上角坐标,可以推得是原始矩阵的每个点乘以3^2得到的,而更大一级矩阵的要摆放的模板是次大一级的矩阵。
所以我们需要做的就是:
1、根据最原始的模板,求出每次的新模板的左上角坐标
2、再摆放位置上摆放旧的模板

  012			
0 # #
1  #
2 # #
  012345678 
0 # #   # #         
1  #     #          
2 # #   # #         
3    # #            
4     #             
5    # #            
6 # #   # #         
7  #     #          
8 # #   # # 

代码如下:

#include<iostream>
#include<cstdio>
#include<math.h>
#include<string>

using namespace std;

char templat [6][6]; //初始模版:
char m2[3000][3000];	//old template
char result[3000][3000]; 	//new template
int len; // new the size of matrix
int n,q; // input n and q level

//该函数的作用是在x,y为左上角坐标的位置摆放字符。
void update(int x,int y,bool flag) {
    // len 应该是此时(模版)的大小
    for (int i = 0;i < len; i++){
       for (int j = 0; j < len; j++){
           if (flag)
               result [x+i][y+j] = m2[i][j]; // 每次摆放一个旧模版大小的图形
           else 
               result [x+i][y+j] = ' ';
           }
       }
}

int main(){
    while(cin >> n){
        // input and output is not in same buffer so,every time you can output the result array;
        if(n == 0) break;
        //2.前面的scanf()在读取输入时会在缓冲区中留下一个字符'\n'(输入完s[i]的值后按回车键所致),
            //所以如果不在此加一个getchar()把这个回车符取走的话,gets()就不会等待从键盘键入字符,
            //而是会直接取走这个“无用的”回车符,从而导致读取有误;
        cin.get(); // take the \n
        for(int i=0;i<n;i++){
                //读入整行数据,它使用回车键输入的换行符来确定输入结尾。
                //调用方法: cin.getline(str (字符数组), len);

            // 获取输入方法2:
            // string temp;
            // getline(cin,temp);
            // for (int j = 0; j < temp.size(); j++)
            //     templat[i][j] = temp[j];

                cin.getline(templat[i],6); // 获取输入方法1

            }

            scanf("%d",&q);
            for(int k=0;k<q;k++){
                len = pow(n,k);//根据len来计算要摆模板的左上角坐标
                // first level:
                if(len == 1){ // 边界条件 如果是k就是0,如果是len就是1
                    for (int i = 0; i < n; i++)
                        for(int j=0;j<n;j++){
                            m2[i][j] = templat[i][j];
                            result[i][j] = m2[i][j];
                        }
                }
                else{
                    // 1.分析模版,按照初始模版来计算位置,进行update(旧模板摆放)
                    for (int i = 0; i < n; i++){
                        for (int j = 0; j < n; j++){
                            if (templat[i][j]==' ')
                                update(i*len,j*len,false);
                            else 
                                update(i*len,j*len,true);
                        }
                    }
                    // 3. 把新的模板作为旧的模板继续摆放:
                    for (int i = 0; i < len*n; i++){
                        for (int j = 0; j < len*n; j++)
                            m2[i][j] = result[i][j];
                        }

                    }
                }

        //输出
        len = pow(n,q);
        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < len; j++)
                cout << result[i][j];

            cout << endl;
        }

    }
    return 0;
}
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值