您的答案為: 1 5 6 1 0 正確答案為: 2 9 3 1 2
您的答案為: 0 9 4 3 1 9 6 5 6 正確答案為: 5 0 7 0 8 1 5 0 3
------------------------------
#coding = UTF-8
import sys
def Cir (row,col,inp):
Tp = []
for m in range (col-1,-1,-1):#旋轉
TpIn=[]
for n in range(row):
TpIn.append(inp[n][m])
Tp.append(TpIn)
return (Tp)
def Updown (row,col,inp):#翻轉
for j in range(col):
k = 0
for i in range(row-1,row//2,-1):
temp = inp[k][j]
inp[k][j] = inp[i][j]
inp[i][j] = temp
k += 1
return (inp)
def Calc(fp,inp,lps):
cnt = 0
for g in range(len(lps)):#決定做幾次旋轉及翻轉
fp[0] = len(inp)
fp[1] = len(inp[0])
if (lps[g]=="0"):#旋轉
inp = Cir(fp[0],fp[1],inp)
cnt += 1
else:#翻轉
inp = Updown(fp[0],fp[1],inp)
if (cnt % 2 ==0):#輸出轉換後矩陣的行數及列數
print (str(sInFp[0])+" "+str(sInFp[1]))
else:
print (str(sInFp[1])+" "+str(sInFp[0]))
for i in inp:#輸出轉換後的矩陣
jsum = ""
for j in i:
jsum += str(j) + " "
print (jsum[:-1])
sIn = sys.stdin.readline()
while (sIn != ""):
sIn = sIn.replace("\r","").replace("\n","")
sInFp = sIn.split()
sInFp = list(map(int,sInFp))
sInp =[]
for i in range(sInFp[0]):#判斷要一次有多少列數的矩陣
sIn = sys.stdin.readline()
sIn = sIn.replace("\r","").replace("\n","")
p = sIn.split()
p = list(map(int,p))
sInp.append(p)
sIn = sys.stdin.readline()#題目給的旋轉0及翻轉1資料列
sIn = sIn.replace("\r","").replace("\n","")
sInLp = sIn.split()
Lpre = []
Lpre = sInLp[::-1]
Calc(sInFp,sInp,Lpre)
sIn = sys.stdin.readline()
目前看到應該是錯在「翻轉」的部分,
對於 for i in range(row-1,row//2,-1): 的部分,
由於這樣寫當 i = row//2 就會跳出 for 迴圈,
即當 i = row//2 時並不會執行交換的動作,
故當列(R)是偶數時最中間的2列並不會被交換, (奇數時中間不交換也沒差)
使用以下測資測試你的程式:
4 1 1
1
2
3
4
1
會輸出:
4 1
4
2
3
1
正確答案應為:
4 1
4
3
2
1
以上是目前發現的問題~
希望有幫助到你~ OwO
感謝Python_in_one_line,幫我釐清問題
我之前一直以為是讀取資料的方式有問題
因為測試會過
後來找到問題後
解決了,謝謝
#coding = UTF-8
import sys
def Cir (row,col,inp):
Tp = []
for m in range (col-1,-1,-1):
TpIn=[]
for n in range(row):
TpIn.append(inp[n][m])
Tp.append(TpIn)
return (Tp)
def Updown (row,col,inp):
for j in range(col):
k = 0
for i in range(row-1,row//2-1,-1):
temp = inp[k][j]
inp[k][j] = inp[i][j]
inp[i][j] = temp
k += 1
return (inp)
def Calc(fp,inp,lps):
for g in range(len(lps)):
fp[0] = len(inp)
fp[1] = len(inp[0])
if (lps[g]=="0"):
inp = Cir(fp[0],fp[1],inp)
else:
inp = Updown(fp[0],fp[1],inp)
print (str(len(inp))+" "+str(len(inp[0])))
for i in inp:
jsum = ""
for j in i:
jsum += str(j) + " "
print (jsum[:-1])
sIn = sys.stdin.readline()
while (sIn != ""):
sIn = sIn.replace("\r","").replace("\n","")
sInFp = sIn.split()
sInFp = list(map(int,sInFp))
sInp =[]
for i in range(sInFp[0]):
sIn = sys.stdin.readline()
sIn = sIn.replace("\r","").replace("\n","")
p = sIn.split()
p = list(map(int,p))
sInp.append(p)
sIn = sys.stdin.readline()
sIn = sIn.replace("\r","").replace("\n","")
sInLp = sIn.split()
Lpre = []
Lpre = sInLp[::-1]
Calc(sInFp,sInp,Lpre)
sIn = sys.stdin.readline()