首页 > 试题广场 >

C翻转

[编程题]C翻转
  • 热度指数:6061 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。 操作类型有四种:  1 2 表示:90度,顺时针,翻转4个数  1 3 表示:90度,顺时针,翻转9个数  2 2 表示:90度,逆时针,翻转4个数  2 3 表示:90度,逆时针,翻转9个数 

输入描述:
输入有多组数据。
每组输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。


输出描述:
输出翻转后的数组。
示例1

输入

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
1 3 1 1

输出

11 6 1 4 5
12 7 2 9 10
13 8 3 14 15
16 17 18 19 20
21 22 23 24 25
while True:
    try:
        c = []
        for _ in range(5):
            c.append(list(input().split()))
        b = list(map(int,input().split()))
        x = b[2] - 1
        y = b[3] - 1
        if b[0] == 1 and b[1] == 2:
            c[x][y],c[x][y+1],c[x+1][y+1],c[x+1][y] = c[x+1][y],c[x][y],c[x][y+1],c[x+1][y+1]
        elif b[0] == 1 and b[1] == 3:
            c[x][y],c[x][y+1],c[x][y+2],c[x+1][y+2],c[x+2][y+2],c[x+2][y+1],c[x+2][y],c[x+1][y]\
            = c[x+2][y],c[x+1][y],c[x][y],c[x][y+1],c[x][y+2],c[x+1][y+2],c[x+2][y+2],c[x+2][y+1]
        elif b[0] == 2 and b[1] == 2:
            c[x][y],c[x][y+1],c[x+1][y+1],c[x+1][y] = c[x][y+1],c[x+1][y+1],c[x+1][y],c[x][y]
        else:
            c[x][y],c[x][y+1],c[x][y+2],c[x+1][y+2],c[x+2][y+2],c[x+2][y+1],c[x+2][y],c[x+1][y]\
            = c[x][y+2],c[x+1][y+2],c[x+2][y+2],c[x+2][y+1],c[x+2][y],c[x+1][y],c[x][y],c[x][y+1]
        for i in c:
            print(' '.join(map(str,i)))
    except:
        break

发表于 2020-04-13 12:03:37 回复(0)
def one(x,y,index):
    list1=[]
    for j in range(y-1,y-1+index):
        re=[]
        for i in range(x-1,x-1+index):
            re.append(arr[i][j])
        list1.append(re[::-1])
    for i in range(x-1,x-1+index):
        for j in range(y-1,y-1+index):
            arr[i][j]=list1[i-x+1][j-y+1]
def two(x,y,index):
    list1=[]
    for j in range(y-2+index,y-2,-1):
        re=[]
        for i in range(x-1,x-1+index):
            re.append(arr[i][j])
        list1.append(re)
    for i in range(x-1,x-1+index):
        for j in range(y-1,y-1+index):
            arr[i][j]=list1[i-x+1][j-y+1]
while True:
    try:
        arr=[]
        for i in range(5):
            arr.append(list(input().strip().split(' ')))
        inp=list(map(int,input().strip().split(' ')))
        if [inp[0],inp[1]]==[1,2]:
            one(inp[2],inp[3],2)
        elif [inp[0],inp[1]]==[1,3]:
            one(inp[2],inp[3],3)
        elif [inp[0],inp[1]]==[2,2]:
            two(inp[2],inp[3],2)
        else:
            two(inp[2],inp[3],3)
        for i in arr:
            print(' '.join(i))
    except:
        break
发表于 2019-08-31 22:42:15 回复(0)

python3解法


import re

a = input()
while True:
    try:
        print(re.sub(r"(?i)" + a, "", input()).replace(" ", ""))
    except:
        break

或者


import re

a = input()
while True:
    try:
        print(re.sub(a, "", input(), flags=re.I).replace(" ", ""))
    except:
        break

两种方法都可以

发表于 2017-10-17 11:07:20 回复(2)
import re
a = raw_input()
try:
    while 1:
        print re.sub(a, '', raw_input(), flags=re.I).replace(' ','')
except:
    pass

发表于 2016-12-25 19:01:31 回复(0)