首页 > 试题广场 >

Repeater

[编程题]Repeater
  • 热度指数:9013 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.
示例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     
1、一倍一倍放大
2、按照起初模板一行一行变大
3、一直对起初模板进行变大,空格变长为模板长度的倍数,符号变为上一倍数对应的行
Python的字符串替换函数挺好用的
try:
    while True:
        num = int(input())
        if num == 0:break
        result = []                     #输出结果
        template = []                   #起初模板(不变)
        for i in range(num):
            template.append(input())
            result.append(template[i])
        sign = result[0].split()[0][0]  #得到符号
        zoomsNum = int(input())
        for i in range(1,zoomsNum):     #i为放大的倍数
            replaceNum = num**i
            example = []                #保存着前一个倍数的模板
            blanks = " "*replaceNum     #当前倍数比较起初模板空格被放大的倍数
            for j in range(num):        #j为起初模板对于的行
                for k in range(replaceNum):       #k是对应起初模板的一行中被放大倍数后的行数
                    if j == 0:
                        example.append(result[k])    #保存着前一个倍数的模板  
                        result[k] = template[j].replace(" ",blanks)         #在起初第一行模板不需要增加行数,所以只需要放大空格和符号sign就好
                    else:
                        result.append(template[j].replace(' ',blanks))
                    signLines = example[k]           #把每一个符号位替换成上一个倍数对应的行
                    result[k+j*replaceNum] = result[k+j*replaceNum].replace(sign,signLines)
        for i in result:
            print(i)
except Exception:
    pass
编辑于 2018-10-04 09:54:39 回复(1)
唔,写了好久。。。
while True:
    try:
        N=int(input())
        if N==0:
            break
        s=[]
        #s为最后输出结果
        sta=[]
        #sta列表储存被替换字符串的模板,长度恒为N,即输入N后的N行
        for n in range(N):
            s.append(input())
            sta.append(s[n])
        for c in s[0]:#找到特殊字符
            if c != ' ':
                ch=c
                break
        Q=int(input())
        lines=N**Q
        for i in range(1,Q):#eg: 3(N)^3(Q) 分解为 3*9 替换
            Len=N**i
            '''每次替换总行数
               Q=2时仅循环一次(i=1),替换N行,
               Q=3时最后一次循环(i=2)替换N^2行'''
            ex=[]
            '''ex列表储存替换字符串的模板
               eg:Q=3时最后一次(i=2),ex有9个元素
               先找到sta[0]中的空格,将其替换成最后输出N^(Q-1)个空格
               再将上述得到的字符串中出现的特殊字符(eg:'#')换为ex[0]
               得到s[0],如此循环N^(Q-1)遍
               再找到sta[1]中的空格,……以此类推
               sta[N-1]……
               得到s[N^Q],即N*N^(Q-1)
            '''
            str0=""
            for h in range(Len):
                str0+=' '#生成一个字符串,包含Len个空格
            for j in range(N): 
                for k in range(Len):
                    if j==0:
                        ex.append(s[k])
                        s[k]=sta[j].replace(' ',str0)
                    else:
                        s.append(sta[j].replace(' ',str0))
                    str1=ex[k]
                    s[k+j*Len]=s[k+j*Len].replace(ch,str1)
                    
        for l in range(lines):#s(最后输出结果),有N^Q行
            print(s[l])
    except:
        break

发表于 2018-08-07 22:04:06 回复(0)

问题信息

难度:
2条回答 8760浏览

热门推荐

通过挑战的用户

查看代码