Repeater(机试题)Python解法

题目:

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. 

输入描述:

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     

代码:

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

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值