飆程式網 Python 題解

飆程式網 ( http://khcode.m-school.tw/ ) 1000~1019 Python 題解
(在解題系統使用 Python解題讀入測試資料方法 附在文末)
題號
上傳解答
離線解答 ( 線下測試 )
#1000
PY2 / PY3:
(
什麼都不用) 直接上傳
PY2 / PY3:
(
什麼都不用) 直接上傳
#1001

--------

#1002

print('Hello World')
print('Hello FHHS')
-----------------------------------------------------
# 飆程式網  #1002
s=int(input())
print(s)

print(s)
print('Hello World')
print('Hello FHHS')
----------------------------------------------------
# 飆程式網  #1002
s=int(input())
print(s)

print(s)
#1003
# 飆程式網  #1003
ipline=input()
print(ipline)
print(ipline)
# 飆程式網  #1003
ipline='FHHS123fhhs'
print(ipline)
print(ipline)
#1004
# 飆程式網  #1004
# x=13
x=int(input())
y = (x**3) + 3*(x**2) + 7*x + 4
print(y)
# 飆程式網  #1004
x=13
# x=int(input())
y = (x**3) + 3*(x**2) + 7*x + 4
print(y)
#1005
# 飆程式網  #1005
ipline =input()
# ipline='3 7 5'
ipline=' '+ipline+' '

numlist=[]
strlen=len(ipline)
c=0 ;k=0 ; l=0;  r=0

for i in range(strlen):
    if ipline[i]==' ':
        l=k; r=i
        k=r
        c=c+1
        if c>1 :
            numlist.append(int(ipline[l+1:r]))

x=numlist[0]
y=numlist[1]
z=numlist[2]
# print(x,y,z)
f= - (x**3) + y*(x**2) - z*x + 4

print(f)
# 飆程式網  #1005-0 
# ipline='3 17 15'
ipline=input()
print(ipline)

t=ipline
# print(t)

# 1st numeber
sl=t.find(' ')
# print(int(t[0:sl]))
x=int(t[0:sl])


# 2nd number
lot=len(t)
t=t[sl+1:lot+1]
sl=t.find(' ')
#print(int(t[0:sl]))
y=int(t[0:sl])

# 3rd number
# print(t)
sl=t.find(' ')
lot=len(t)
t=t[sl+1:lot]
#print(t)
z=int(t)

f= - (x**3) + y*(x**2) - z*x + 4
print(f)

------------------------------------------------
# 1005-1
#ipline =input()
ipline='13 17 15'
a= list(map(int,ipline.split(' ')))

x=a[0]
y=a[1]
z=a[2]
f= - (x**3) + y*(x**2) - z*x + 4
print(f)
#1006
# 飆程式網  #1006
x=int(input())
# x=5
if x>0 :
    f= x**3 + 3*x**2 + 7*x + 4
if x<= 0:
    f= 2*(x**3 )- x**2 - 2
print(f)
# 飆程式網  #1006
# x=input()
x=5
if x>0 :
    f= x**3 + 3*x**2 + 7*x + 4
if x<= 0:
    f= 2*(x**3 )- x**2 - 2
print(f)
#1007
# 飆程式網  #1007
x=int(input())
# x=5
if x>0 :
    f= x**3 - 311*(x**2)  - 72*x-4
if x<= 0:
    f= 2*(x**3 ) + 78 *(x**2) - 2

print( abs(f) // 10007 )
print( abs(f) % 10007)
# 飆程式網  #1007
# x=int(input())
x=5
if x>0 :
    f= x**3 - 311*(x**2)  - 72*x-4
if x<= 0:
    f= 2*(x**3 ) + 78 *(x**2) - 2

print( abs(f) // 10007 )
print( abs(f) % 10007)
#1008
# 飆程式網  #1008
x=int(input())
y=int(input())
# x=17
# y=1234
f= 2*(x**5) + 4 * (x**3 ) +7
print(f % y)
# 飆程式網  #1008
# x=int(input())
x=17
y=1234

f= 2*(x**5) + 4 * (x**3 ) +7
print(f % y)
#1009
# 飆程式網  #1009
ipline=input()
# ipline='Wen'

if ord(ipline[0]) > 64 and ord(ipline[0]) < 91 :
    print('YES')
else:
    print('No')
# 飆程式網  #1009
# ipline=input()
ipline='Wen'

if(ipline[0].isupper() == True ):
    print("YES")
else:
    print("No")
#1010
# 飆程式網1010 : 找出最小值
n=int(input())
ipline=input()
# n=5
# ipline='6 3 4 17 9'
ipline=' '+ipline+' '

numlist=[]
strlen=len(ipline)
c=0 ;k=0 ; l=0;  r=0

for i in range(strlen):
    if ipline[i]==' ':
        l=k; r=i
        k=r
        c=c+1
        if c>1 :
            numlist.append(int(ipline[l+1:r]))
print(min(numlist))
#飆程式網1010 : 找出最小值
# n=int(input())
# ipline=input()
n=5
ipline='6 3 4 17 9'
ipline=' '+ipline+' '

numlist=[]
strlen=len(ipline)
c=0 ;k=0 ; l=0;  r=0

for i in range(strlen):
    if ipline[i]==' ':
        l=k; r=i
        k=r
        c=c+1
        if c>1 :
            numlist.append \
            (int(ipline[l+1:r]))
print(min(numlist))
#1011
# 陣列反轉 (飆程式網 #1011) -1
n=int(input())
ipline=input()
a = ipline.split(' ')
print(' '.join(reversed(a)))
# 陣列反轉 (飆程式網 #1011) -1
n=5
ipline="2 3 12 4 5"
a = ipline.split(' ')
print(' '.join(reversed(a)))
#1012
# 飆程式網  #1012
n=int(input())   
for i in range(n):
    ipline=input()
    print(ipline,end=' ')
    for j in range(10):
        c=ipline.count(str(j))
        print(c,end=' ')
    print()
# 飆程式網  #1012 ( 離線題解 )
n=5
iplist=["0","00","5140514", \
        "9999999999","1234567890"]
for i in range(n):
    print(iplist[i],end=' ')
    for j in range(10):
        c=iplist[i].count(str(j))
        print(c,end=' ')
        c=0           
    print()
#1013
# 飆程式網  #1013
ipline=input()
for i in range(len(ipline)-1,-1,-1):
    print(ipline[i:i+1],end='')
# 飆程式網  #1013
ipline="FHHS"
for i in range(len(ipline)-1,-1,-1):
    print(ipline[i:i+1],end='')
#1014
# 飆程式網  #1014
ipline=input()
# ipline="FHHs"
cc=0 ; cl =0
for i in range(len(ipline)):
    if ipline[i:i+1] >='A'  and ipline[i:i+1] <='Z' :
        cc=cc+1
    elif ipline[i:i+1] >='a'  and ipline[i:i+1] <='z' :
        cl=cl+1
print(cl,cc)
# 飆程式網  #1014
# ipline=input()
ipline="Fhhs"
cc=0 ; cl =0
for i in range(len(ipline)):
    #  print(ipline[i:i+1])
    if ipline[i:i+1] >='A' and ipline[i:i+1] <='Z' :
        cc=cc+1
    elif ipline[i:i+1] >='a' and ipline[i:i+1] <='z' :
        cl=cl+1
print(cc,cl)
#1015
# 飆程式網  #1015
ipline=input()
# ipline='AbCdEfzZ'
str=''
for i in range(len(ipline)):
    # print(ipline[i:i+1], ord(ipline[i:i+1])) 
    if ord(ipline[i:i+1]) >=65 and ord(ipline[i:i+1]) <=90:
        str=str+chr(ord(ipline[i:i+1])+32)
    elif ord(ipline[i:i+1]) >=97 and ord(ipline[i:i+1]) <=123:
        str=str+chr(ord(ipline[i:i+1])-32)
print(str)
# 飆程式網  #1015
#ipline=input()
ipline='AbCdEfzZ'
str=''
for i in range(len(ipline)):
    print(ipline[i:i+1], ord(ipline[i:i+1]))   
    if ord(ipline[i:i+1]) >=65 and ord(ipline[i:i+1]) <=90:
        str=str+chr(ord(ipline[i:i+1])+32)
    elif ord(ipline[i:i+1]) >=97 and ord(ipline[i:i+1]) <=123:
        str=str+chr(ord(ipline[i:i+1])-32)
print(str)
#1016
# 飆程式網  #1016
n=input()
ipline=input()
# n=13
# ipline="1 9 3 5 1 4 0 9 8 1 4 2 4"
c=[]
for i in range(10):
    c.append(0)
#  print(c)

for i in range(0,len(ipline),2):
    n=int(ipline[i]) ; # print(n, end='')
    c[n]=c[n]+1
         
# print(c); print(max(c))
for i in range(10):
    if c[i] == max(c) :
        print(i,end=' ')
# 飆程式網  #1016
# n=input()
# ipline=input()
n=13
ipline="1 9 3 5 1 4 0 9 8 1 4 2 4"
c=[]
for i in range(10):
    c.append(0)
#  print(c)

for i in range(0,len(ipline),2):
    n=int(ipline[i]) ; # print(n, end='')
    c[n]=c[n]+1           

# print(c); print(max(c))
for i in range(10):
    if c[i] == max(c) :
        print(i,end=' ')
#1017
# 飆程式網  #1017
ipline01=input()
ipline02=input()
# ipline01='4 1 2 3 4'
# ipline02='3 1 10 100'

c=[]
for i in range(10):
    c.append(0)

list01=list(map(int,ipline01.split())); list02=list(map(int,ipline02.split()))
for i in range(1,list02[0]+1):

    for j in range(1,list01[0]+1):
        print(list01[j]*list02[i],end=' ')
    print()
# 飆程式網  #1017
# ipline=input()

ipline01='4 1 2 3 4'
ipline02='3 1 10 100'

c=[]
for i in range(10):
    c.append(0)

list01=list(map(int,ipline01.split())); list02=list(map(int,ipline02.split()))
for i in range(1,list02[0]+1):

    for j in range(1,list01[0]+1):
        print(list01[j]*list02[i],end=' ')
    print()
#1018
# 飆程式網  #1018
n=int(input())
ipline=[]
for i in range(n):
    ipline.append(input())

for i in range(n-1,-1,-1):
    print(ipline[i])
# 飆程式網  #1018
n=3
ipline=('FHHS','meow','NTU')

for i in range(n-1,-1,-1):
    print(ipline[i])
#1019
# 飆程式網  #1019
# iplinemn='4 3'
iplinemn=input()
listmn=list(map(int,iplinemn.split()))
# print(listmn)
m= listmn[0]
n= listmn[1]
# iplist=['10 2 3','4 5 6','7 8 9','5 1 4']

listA=[]
for i in range(m):
    # print(iplist[i])
    list01=list(map(int,input().split()))
    listA.append(list01)
# print(listA)
for i in range(n):
    for j in range(m):
        print(listA[j][i],end=' ')
    print()
# 飆程式網  #1019
iplinemn='4 3'

listmn=list(map(int,iplinemn.split()))
# print(listmn)
m= listmn[0]
n= listmn[1]
iplist=['10 2 3','4 5 6','7 8 9','5 1 4']

listA=[]
for i in range(m):
    # print(iplist[i])
    list01=list(map(int,iplist[i].split()))
    listA.append(list01)
# print(listA)
for i in range(n):
    for j in range(m):
        print(listA[j][i],end=' ')
    print()
#1020
# 飆程式網  #1020
n=int(input())
# n=12
for i in range (1,n+1):
    if ( n % i )==0 :
        print(i)
# 飆程式網  #1020
# n=int(input())
n=12
for i in range (1,n+1):
    if ( n % i )==0 :
        print(i)
#1021
# 飆程式網  #1021
# n=4
# iplist=['SHHS','1234321','meow','aBcDDcBa']
n=int(input())
for i in range(n):
    strline=input() ;  # print( strline)
    strlen=len(strline) ; # print(strlen)
    chk=(strlen // 2) ; # print(chk)
    f=1
    for j in range(chk):
        # print(j,strline[j:j+1],strline[strlen-j-1:strlen-j])
        if strline[j:j+1] != strline[strlen-j-1:strlen-j] :
            f=0
    if f==1 :
        print("Yes")
    else :
        print("No")        
# 飆程式網  #1021
n=4
iplist=['SHHS','1234321','meow','aBcDDcBa']
for i in range(n):
    strline=iplist[i] ;  # print( strline)
    strlen=len(strline) ; # print(strlen)
    chk=(strlen // 2) ; # print(chk)
    f=1
    for j in range(chk):
        # print(j,strline[j:j+1],strline[strlen-j-1:strlen-j])
        if strline[j:j+1] != strline[strlen-j-1:strlen-j] :
            f=0
    if f==1 :
        print("Yes")
    else :
        print("No")       
#1022
# 飆程式網  #1022
# ipline='abccdhhyyky'
ipline=input()
ipline=' '+ipline+' ' # 前後二字都可比對
c = 0
for i in range (1,len(ipline)-1):
    if ipline[i] == ipline[i+1] or ipline[i]==ipline[i-1]:
        c=c+1
print(c)
# 飆程式網  #1022
ipline='abccdhhyyky'
ipline=' '+ipline+' ' # 前後二字都可比對
c = 0
for i in range (1,len(ipline)-1):
    if ipline[i] == ipline[i+1] or ipline[i]==ipline[i-1]:
        c=c+1
print(c)

#1023
# 飆程式網  #1023
ipline=input()
iplist=list(map(int,ipline.split()))
listA=sorted(iplist)
strline=str(listA[0])+' '+str(listA[1])+' '+str(listA[2])
print( strline)
# 飆程式網  #1023
ipline='5 1 4'
iplist=list(map(int,ipline.split()))
print(iplist)
listA=sorted(iplist)
print(listA)
strline=str(listA[0])+' '+str(listA[1])+' '+str(listA[2])
print( strline)
#1023
(另解)
# 飆程式網  #1023-1
iplist=list(map(str,input().split()))
iplist.sort()
newstr = ' '.join(iplist)
print(newstr)
# 飆程式網  #1023-1
ipline='5 1 4'
iplist=list(map(str,ipline.split()))
iplist.sort()
newstr = ' '.join(iplist)
print(newstr)
#1024
# 飆程式網  #1024
# n=1024
n=int(input())
for x in range( n // 2):
    for y in range ( n //2):
        if x ** y == n :
            print(x,y)
# 飆程式網  #1024
n=1024
# n=int(input())
for x in range( n // 2):
    for y in range ( n //2):
        if x ** y == n :
            print(x,y)
#1025
# 矩陣計算 1025

n = int(input())
A = []
for i in range(n):
    A.append([int(elem) for elem in input().split()])

B = []
for i in range(n):
    B.append([int(elem) for elem in input().split()])
   
C = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        for k in range(n):
            C[i][j] += A[i][k] * B[k][j]
       
for i in range(n):
    print(' '.join(str(elem) for elem in C[i]))

# 矩陣計算 1025
# https://matrixcalc.org/zh/#%7B%7B7,4%7D,%7B3,5%7D%7D%2A%7B%7B2,1%7D,%7B4,3%7D%7D

n = int(input())
A = []
for i in range(n):
    A.append([int(elem) for elem in input().split()])

B = []
for i in range(n):
    B.append([int(elem) for elem in input().split()])
   
C = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        for k in range(n):
            C[i][j] += A[i][k] * B[k][j]
       
for i in range(n):
    print(' '.join(str(elem) for elem in C[i]))







在解題系統使用 Python解題讀入測試資料
    sys.stdin input() 標準輸入:
      解題系統( : ZeroJudge 飆程式網) Python解題讀入測試資料說明:
  1. 線上解題系統的題目中一般都有含3個測試資料,當你把程式寫好以後送到解題系統系統上應該還有56筆測試資料,Python 讀取測試資料的方法有兩種:
(1). input()           # 簡單易用
            
(2). import sys     #  標準輸入函數
                  sInput=sys.stdin.readline().strip()    #
上面兩個指令基本上都是可以從標準輸入設備讀讀入測試資料,資料又分單筆資料和連續資料,連續資料必須由使用者想辦法分割成單筆資料,以便程式執行時使用輸入的數據。
5
 1 12 123 1234 12345
上面兩行: 上面一行是單筆資料,下面一行是五個連續資料,程式撰寫者應該具備把單行連續資料拆解成單筆資料,例如把第二行拆解成串列:
[1, 12, 123, 1234, 12345]

2.   使用  input()  拆解連續資料成單筆資料
[範例程式]       ( 0-input().py )
n=int(input('input 單筆數字 n:\n'))
list01=list(map(int,input('input多筆數字 n:\n').split()))
print(n)
print(list01,'\n')

[執行結果]
input 單筆數字 n:
12
input 多筆數字 n:
1 3 5 7 9 11 13
12
[1, 3, 5, 7, 9, 11, 13]  

3.    使用  sys.stdin.read 拆解連續資料成單筆資料
python中的sys.stdin 有二種指令:
(1). sys.stdin.readline() 僅接受一行的全部輸入
可在末尾加上.strip().strip(“\n”)去掉末尾的換行符, :
line=sys.stdin.readline().strip()     #末尾加.strip() ,去掉了換行符
(2). sys.stdin.read()可以接受多行的標準輸入,包括末尾的'\n'      

使用 sys.stdin 解題
==================================
1000 [語言] 第一支程式
---------------------------------
(
問題敘述 )
只要能夠成功編譯,就能獲得滿分,超好賺的
(
不必做任何計算, 也不能做任何輸入、輸出!)
-----------------------------------
PY2 / PY3:
(
什麼都不用) 直接上傳
==================================
1001 [
語言] 輸出操作
-------------------------
(
問題敘述 )
你的程式能輸出上古時代的範例「 Hello World 」嗎?
就從這題開始吧~~
----------------------------------------
(
)
print('Hello World')
print('Hello FHHS')
==================================
1003 [語言] 字串之輸入與輸出操作
-----------------------------------
(
問題敘述 )
來試試字串的輸入以及輸出吧!
輸入共有一列,包含一個由英文字母大小寫與數字組成的字串 S,字串的長度至少為1

測試資料 1,字串的長度不超過10
測試資料 2,字串的長度不超過100
測試資料 3,字串的長度不超過10000
測試資料 4,字串的長度不超過1000000
--------------------------------------
(
)
import sys

sInput=sys.stdin.readline().strip()

while(sInput!=""):
    print(sInput)
    print(sInput)
    sInput=sys.stdin.readline().strip()


==================================
1004 [語言] 簡單的數值運算
----------------------------------------
(
問題敘述 )
來試試看用程式語言當計算機好了
請依照輸出格式輸出計算後所得的答案
f(x) = x^3 + 3x^2 + 7x + 4

(
上式中的 a^b 表示 a b 次方之意,
優先順序較加法與係數運算高,
也即 f(x) = (x^3) + 3(x^2) + 7(x) + 4)

真的這麼簡單嗎?相信真的很簡單XD
不過如果答案錯誤別緊張,會有厲害的老師來幫你!


輸入說明
輸入共有一列
其中包含正整數 X
(X <= 1000000)

對於占分至少 60% 的測試資料,X < 1250
-------------------------------------------
(
)
import sys
for s in sys.stdin:
    s=int(s)
    print(int(s**3+3*(s**2)+7*s+4))

==================================
1005 [語言] 複雜的數值運算
-----------------------------------------
(
問題敘述 )
來試試看用程式語言當計算機好了
請依照輸出格式輸出計算後所得的答案
f(x,y,z) = - (x^3) + y*(x^2) - z*x + 4

(
上式中的 a^b 表示之意,與題目1004相同)

輸入說明
輸入共有一列
其中包含整數 X, Y, Z,以一個空白分隔。
(|X|,|Y|,|Z| <= 1000000, |X|
表示 X 的絕對值)
-----------------------------------------
(
)
import sys
for i in sys.stdin:
    x,y,z=map(int,i.split())
    print((-(x**3)+(y*(x**2))-(z*x)+4))

==================================
1006 [語言] 含條件判斷之數值運算
----------------------------------------
(
問題敘述 )
傳說中的「If」敘述讓程式能在所判斷的各種情況執行不同的流程

f(x) 由以下程序定義:
(a)
x > 0, f(x) = x^3 + 3x^2 + 7x + 4
(b)
x <= 0, f(x) = 2x^3 - x^2 - 2

輸入說明
輸入共有一列
其中包含整數 X
(|X| <= 1000)
-----------------------------------------
(
)
import sys

sInput=sys.stdin.readline().strip()

while(sInput!=""):
    x = int(sInput)
    if(x>0):
        print(x**3+3*x**2+7*x+4)
    else:
        print(2*x**3-x**2-2)
    sInput=sys.stdin.readline().strip()

==================================

1007 [語言] 復刻版數值運算
-----------------------------------------
(
問題敘述 )
f(x) 由以下程序定義:
(a)
x > 0, f(x) = x^3 - 311x^2 - 72x - 4
(b)
x <= 0, f(x) = 2x^3 + 78x^2 - 2


輸入說明
輸入共有一列
其中包含整數 X
(|X| <= 1000)

-----------------------------------------
(
)
import sys

sInput=sys.stdin.readline().strip()

while(sInput!=""):
    x = int(sInput)
    if(x>0):
        print(abs(x**3-311*(x**2)-72*x-4)//10007)
        print(abs(x**3-311*(x**2)-72*x-4)%10007)
    else:
        print(abs(2*x**3+78*(x**2)-2)//10007)
        print(abs(2*x**3+78*(x**2)-2)%10007)
    sInput=sys.stdin.readline().strip()

==================================
1008 [語言] 餘數版數值運算
---------------------------------------------
(
問題敘述 )
f(x) = 2x^5 + 4x^3 + 7

輸入說明
輸入共有兩列
第一列為正整數X (X <= 1000000000)
第二列為正整數Y (Y <= 1000000000)
-----------------------------------------
(
)
import sys

sInput=sys.stdin.readline().strip()

while(sInput!=""):
    x=int(sInput)
    sInput=sys.stdin.readline().strip()
    y=int(sInput)
    print((2*x**5+4*x**3+7)%y)
    sInput=sys.stdin.readline().strip()

==================================
1009 [語言] 字串開頭之判斷
--------------------------------------
(
問題敘述 )
溫教授在紙上寫了一串由大小寫英文字母組成的連續字串
你可以幫他檢查第一個字母是否為大寫嗎?


輸入說明
輸入共有一列,包含一個由大小寫英文字母組成的連續字串,長度介於110之間。

-----------------------------------------
(
)
import sys

sInput=sys.stdin.readline().strip()

while(sInput!=""):
    if(sInput[0].isupper()==True):
        print("YES")
    else:
        print("No")
    sInput=sys.stdin.readline().strip()

==================================

沒有留言:

張貼留言