Snakify
SNAKIFY
CHAPTER 1
Sum of three numbers
1
2
3
4
| a = int(input())
b = int(input())
c = int(input())
print(a + b + c)
|
後記: input()出來是字串,要轉形
Hi John
1
2
| a=input()
print("Hi ",a)
|
後記: Python的print預設是間隔一空白(如果要改預設可用sep 或+ 處理),並換行
Square
1
2
| a= int(input())
print(a*a)
|
Area of right-angled triangle
1
2
3
| b = int(input())
h = int(input())
print ( b*h/2 )
|
後記: Python這樣出來可以是float,不須管形別轉換,但C++要注意會不會強制轉換
Hello, Harry!
1
2
| name = input()
print("Hello",name,"!",sep='')
|
後記: 2行可改成 print('Hello, ' + input() + '!')
Apple sharing
1
2
3
4
| n = int(input())
K = int(input())
print(K//n)
print(K%n)
|
後記: //
是整除
Previous and next
1
2
3
| a= int(input())
print("The next number for the number",a,"is",a+1)
print("The previous number for the number",a,"is",a-1)
|
Two timestamps
1
2
3
4
5
6
7
| a=int(input())
b=int(input())
c=int(input())
d=int(input())
e=int(input())
f=int(input())
print((d-a)*60*60+(e-b)*60+(f-c))
|
School desks
1
2
3
4
5
6
7
| import math
sum=0
for i in range(3):
a=int(input())
a=math.ceil(a/2)
sum=a+sum
print(sum)
|
CHAPTER 2
Last digit of integer
1
2
| a=int(input())
print(a%10)
|
Tens digit
1
| print(int(int(input())/10)%10)
|
Sum of digits
正常解法
1
2
3
4
5
| a= int(input())
frist=int(a/100)
second=int((a-frist*100)/10)
third=a%10
print(frist+second+third)
|
字串解
1
2
3
4
5
| a=input()
sum=0
for i in a:
sum+=ord(i)-48
print(sum)
|
一行流
1
| print(sum([ord(i)-48 for i in input()]))
|
後記: 可再延伸UVA題目: 10019 - Funny Encryption Method
Fractional part
理想的解法
1
2
3
| a=float(input())
b=int (a)
print(a-b)
|
實際解法
1
2
3
4
5
| a=input().split(".")
if (len(a)==2):
print('0.'+a[1])
else:
print(0)
|
後記: Float不精確,要用字串處理才能得到標準答案
First digit after decimal point
正常解法
1
2
3
4
5
| a=float(input())
b=float(int(a))
c=float((a-b)*10)
d=int(c)
print(d)
|
字串處理
1
2
3
4
5
| a=input()
if( a.find(".")==-1 ):
print(0)
else:
print(a[a.find(".")+1])
|
精簡化
1
2
| a=input()
print( (a[a.find(".")+1],0) [a.find(".")==-1] )
|
Car route
1
2
3
4
5
6
| N=int(input())
M=int(input())
Day=M//N
if (M%N>0):
Day=Day+1
print(Day)
|
Snakify 解答
1
2
3
4
| import math
N=int(input())
M=int(input())
print(math.ceil(M/N))
|
Digital clock
1
2
| a=int(input())
print(a//60,a%60)
|
Total cost
1
2
3
4
5
| A=int(input())
B=int(input())
N=int(input())
dollar=(A*100+B)*N
print(dollar//100,dollar%100)
|
後記: Float不精確,改用int方式解
Clock face - 1
1
2
3
4
5
| H=int(input())
M=int(input())
S=int(input())
angle=float(H*30+M/2+S/120)
print(angle)
|
Clock face - 2
1
2
| degree=float(input())
print(degree*12%360)
|
後記: hour hand: min hand = 1 : 12
CHAPTER 3
Minimum of two numbers
1
2
3
4
5
6
| a=int(input())
b=int(input())
if (a>b):
print(b)
else:
print(a)
|
簡化
1
2
3
| a=int(input())
b=int(input())
print((a,b)[a>b])
|
Sign function
1
2
3
4
5
6
7
| a=int(input())
if(a>0):
print(1)
elif (a<0):
print(-1)
else:
print(0)
|
Minimum of three numbers
1
2
3
4
5
6
7
8
9
10
| A=[0,0,0]
A[0]=int(input())
A[1]=int(input())
A[2]=int(input())
for i in range(2):
if (A[i+1]>A[i]):
tmp=A[i]
A[i]=A[i+1]
A[i+1]=tmp
print(A[2])
|
後記: Python 數值交換可以改成a,b=b,a,以元組形式
Equal numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| a=int(input())
b=int(input())
c=int(input())
if (a==b):
if (b==c):
print(3)
else:
print(2)
elif (b==c):
if (a==b):
print(3)
else:
print(2)
elif (c==a):
if (a==b):
print(3)
else:
print(2)
else:
print(0)
|
Rook move
1
2
3
4
5
6
7
8
9
| A=[0,0,0,0]
for i in range(4):
A[i]=int(input())
if (A[0]==A[2]):
print("YES")
elif (A[1]==A[3]):
print("YES")
else:
print("NO")
|
Chess board - same color
1
2
3
4
5
6
7
| A=[0,0,0,0]
for i in range(4):
A[i]=int(input())
if ((A[0]+A[1])%2 == (A[2]+A[3])%2):
print("YES")
else:
print("NO")
|
思路: 同顏色 $x+y$ 奇數(偶數)
King move
1
2
3
4
5
6
7
| A=[0,0,0,0]
for i in range(4):
A[i]=int(input())
if (abs(A[0]-A[2])<=1 and abs(A[1]-A[3])<=1):
print("YES")
else:
print("NO")
|
後記: Python可以使用類似數學區間的表達式(-1<=A[0]-A[2]<=1)但C++不行,請注意
Bishop moves
1
2
3
4
5
6
7
| A=[0,0,0,0]
for i in range(4):
A[i]=int(input())
if (abs(A[0]-A[2]) == abs(A[1]-A[3])):
print("YES")
else:
print("NO")
|
後記: abs()
是取絕對值
Queen move
1
2
3
4
5
6
7
8
9
10
11
| A=[0,0,0,0]
for i in range(4):
A[i]=int(input())
if (abs(A[0]-A[2]) == abs(A[1]-A[3])): #斜向移動
print("YES")
elif (A[0]==A[2]): #水平移動
print("YES")
elif (A[1]==A[3]): #鉛直移動
print("YES")
else:
print("NO")
|
Knight move
1
2
3
4
5
6
7
8
9
10
11
| A=[0,0,0,0]
for i in range(4):
A[i]=int(input())
a=abs(A[0]-A[2])
b=abs(A[1]-A[3])
if (a==2 and b==1):
print("YES")
elif (a==1 and b==2):
print("YES")
else :
print("NO")
|
Chocolate bar
1
2
3
4
5
6
7
| n=int(input())
m=int(input())
k=int(input())
if ((k%n==0 or k%m==0) and m*n>=k):
print("YES")
else:
print("NO")
|
後記: 這題多考慮兩種情況
1.長或寬可以整除所求
2.面積大於所求
Leap year
1
2
3
4
5
6
7
| year=int(input())
if (year%4==0 and year%100!=0):
print("LEAP")
elif (year%400==0):
print("LEAP")
else:
print("COMMON")
|
CHAPTER 4
Series - 1
1
2
3
4
| a=int(input())
b=int(input())
for i in range (a,b+1):
print(i)
|
Series - 2
1
2
3
4
5
6
7
8
| a=int(input())
b=int(input())
if (b>=a):
for i in range(a,b+1):
print (i)
elif (a>b):
for i in range(a,b-1,-1):
print(i)
|
Sum of ten numbers
1
2
3
4
5
| sum=0
for i in range(10):
a=int(input())
sum=sum+a
print(sum)
|
Sum of N numbers
1
2
3
4
5
6
| N=int(input())
sum=0
for i in range(N):
a=int(input())
sum=sum+a
print(sum)
|
Sum of cubes
正常作法
1
2
3
4
5
| N=int(input())
sum=0
for i in range(N+1):
sum=sum+(i**3)
print(sum)
|
一行流
1
| print(sum(map(lambda x:x**3,range(1,int(input())+1))))
|
思路: 用range()產生list並用map執行迭代,並回傳陣列給sum()並印出總和
Factorial
正常做法
1
2
3
4
5
6
7
| a=int(input())
b=a
mul=1
for i in range (b):
mul=mul*a
a=a-1
print(mul)
|
遞迴演算法
1
2
| f=lambda x: 1 if x==1 else x*f(x-1)
print( f(int(input())) )
|
The number of zeros
1
2
3
4
5
6
7
| sum=0
N=int(input())
for i in range(N):
a=int(input())
if (a==0):
sum=sum+1
print(sum)
|
Adding factorials
正常作法
1
2
3
4
5
6
7
8
| N=int(input())
sum=0
for i in range(1,N+1,1):
mul=1
for g in range (1,i+1,1):
mul=mul*g
sum=sum+mul
print(sum)
|
一層迴圈做法
1
2
3
4
5
6
7
| N=int(input())
sum=0
mul=1
for i in range(1,N+1,1):
mul=mul*i
sum=sum+mul
print(sum)
|
遞迴演算法與Sum of Club應用
1
2
| f=lambda x: 1 if x==1 else x*f(x-1)
print(sum([f(int(i)) for i in range(1,int(input())+1)]))
|
Ladder
正常作法
1
2
3
4
5
| N=int(input())
for i in range(1,N+1,1):
for g in range(1,i+1,1):
print(g,end="")
print()
|
一行流
1
| for i in range(1,int(input())+1):print("".join([str(j) for j in range(1,i+1)]))
|
思路: 正常解法簡化即可
後記: join
只能用char
,int
不行
Lost card
標記解法
1
2
3
4
5
| N=int(input())
A=[i for i in range(1,N+1)]
for i in range(N-1):
A.remove(int(input()))
print( A[0] )
|
特殊解
1
2
3
4
5
| N=int(input())
sum=0
for i in range (1,N):
sum+=int(input())
print(int(N*(1+N)/2-sum))
|
CHAPTER 5
The number of words
1
2
3
4
5
6
7
8
9
10
| s=input()
print(s[2])
print(s[len(s)-2])
print(s[0:5])
print(s[0:len(s)-2])
print(s[0:len(s):2])
print(s[1:len(s):2])
print(s[-1:-(len(s)+1):-1])
print(s[-1:-(len(s)+1):-2])
print(len(s))
|
The number of words
1
2
| a=input().split(" ")
print(len(a))
|
The two halves
Slice
1
2
| a=input()
print(a[(len(a)+1)//2:]+a[:(len(a)+1)//2])
|
我的解法
1
2
3
4
5
6
7
8
9
10
11
12
| a=input()
a=list(a)
if (len(a)%2==0):
for i in range(int(len(a)/2),len(a),1):
print(a[i],end="")
for i in range(0,int(len(a)/2),1):
print(a[i],end="")
else:
for i in range(int(len(a)/2)+1,len(a),1):
print(a[i],end="")
for i in range(0,int(len(a)/2)+1,1):
print(a[i],end="")
|
To swap the two words
1
2
3
| s=input().split(" ")
for i in range(len(s)-1,-1,-1):
print(s[i],end=" ")
|
The first and last occurrence
1
2
3
4
5
6
7
8
9
10
11
| s=input()
address=[]
for i in range(0,len(s),1):
if (s[i]=='f'):
address.append(i)
if (len(address)==1):
print(address[0])
elif(len(address)==0):
pass
else:
print(address[0],address[len(address)-1])
|
The second occurrence
1
2
3
4
5
6
7
8
9
10
11
| s=input()
address=[]
for i in range(0,len(s),1):
if (s[i]=='f'):
address.append(i)
if (len(address)==1):
print(-1)
elif(len(address)==0):
print(-2)
else:
print(address[1])
|
Remove the fragment
find()
1
2
| s=input()
print(s[ :s.find('h') ]+s[ s.rfind('h')+1: ] )
|
儲存成陣列
1
2
3
4
5
6
7
| s=input()
b=[]
for i in range(0,len(s),1):
if (s[i]=='h'):
b.append(i)
print(s[0:b[0]]+s[b[len(b)-1]+1: ])
|
Reverse the fragment
1
2
3
4
5
6
7
| s=input()
b=[]
for i in range(0,len(s),1):
if (s[i]=='h'):
b.append(i)
print(s[0:b[0]]+s[b[len(b)-1]-len(s):b[0]-len(s):-1]+s[b[len(b)-1]: ])
|
Replace the substring
1
2
3
4
5
6
| s=input()
for i in s:
if (i=='1'):
print("one",end="")
else:
print(i,end="")
|
Delete a character
Snakify 作法
1
| print(input().replace('@',''))
|
我的解法
1
2
3
4
5
6
| s=input()
for i in s:
if (i=='@'):
pass
else:
print(i,end="")
|
Replace within the fragment
用find()
1
2
3
4
5
6
7
8
9
10
| s=input()
l,r=(s.find('h'),s.rfind('h'))
for i in range(len(s)):
if (s[i]=='h'):
if(i!=l and i!=r):
print('H',sep='',end='')
else:
print('h',sep='',end='')
else:
print(s[i],sep='',end='')
|
用count()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| s=input()
count=0
h=s.count('h')
for i in s:
if (i=='h'):
if (count==0):
count+=1
print(i,end="")
continue
elif (count==h-1):
count+=1
print(i,end="")
continue
print("H",end="")
count+=1
else:
print(i,end="")
|
思路: 第一個h和最後一個h不能改,可以用判斷第幾個h的方式執行,再者碰到第一個與最後一個h以continue的方式跳過
Delete every third character
1
2
3
4
5
6
| s=input()
for i in range(0,len(s),1):
if (i%3==0):
pass
else:
print(s[i],end="")
|
CHAPTER 6
:::success
while迴圈
- 用在不確定次數的時候
- 裡面條件是TURE(1)會執行
:::
List of squares
1
2
3
4
5
| a=int(input())
i=1
while (a>=i**2):
print(i**2)
i=i+1
|
Least divisor
1
2
3
4
5
| n=int(input())
i=2
while(n%i!=0):
i=i+1
print(i)
|
The power of two
1
2
3
4
5
6
7
8
9
| n=int(input())
i=0
x=1
while (n>x):
x=x*2
i=i+1
if(x*2>n):
break
print(i,x)
|
Morning jog
1
2
3
4
5
6
7
| x=int(input())
y=int(input())
Day=1
while(x < y):
x=x*1.1
Day=Day+1
print(Day)
|
The length of the sequence
1
2
3
4
5
6
| i=0
a=int(input())
while(a!=0):
a=int(input())
i+=1
print(i)
|
The sum of the sequence
1
2
3
4
5
6
| a=int(input())
sum=0
while(a!=0):
sum=a+sum
a=int(input())
print(sum)
|
The average of the sequence
1
2
3
4
5
6
7
8
| count=0
a=int(input())
sum=0
while(a!=0):
sum=sum+a
a=int(input())
count=count+1
print(sum/count)
|
The maximum of the sequence
1
2
3
4
5
6
7
| a=int(input())
max=a
while(a!=0):
if(a>max):
max=a
a=int(input())
print(max)
|
The index of the maximum of a sequence
1
2
3
4
5
6
7
8
9
10
11
| address=1
max_address=1
a=int(input())
max=a
while(a!=0):
if(a>max):
max=a
max_address=address
a=int(input())
address+=1
print(max_address)
|
後記: 類似於氣泡排序
The number of even elements of the sequence
1
2
3
4
5
6
7
| a=int(input())
count=0
while(a!=0):
if (a%2==0):
count+=1
a=int(input())
print(count)
|
The number of elements that are greater than the previous one
1
2
3
4
5
6
7
8
9
| a=int(input())
tmp=a
count=0
while(a!=0):
a=int(input())
if (a>tmp):
count+=1
tmp=a
print(count)
|
The second maximum
Snakify解法(以兩變數實現)
1
2
3
4
5
6
7
8
9
10
11
12
| first_max = int(input())
second_max = int(input())
if first_max < second_max:
first_max, second_max = second_max, first_max
element = int(input())
while element != 0:
if element > first_max:
second_max, first_max = first_max, element
elif element > second_max:
second_max = element
element = int(input())
print(second_max)
|
本人解法(list實現)
1
2
3
4
5
6
7
| n=int(input())
b=[]
while(n!=0):
b.append(n)
n=int(input())
b.sort()
print(b[len(b)-2])
|
The number of elements equal to the maximum
1
2
3
4
5
6
7
8
9
10
11
| n=int(input())
b=[]
while(n!=0):
b.append(n)
n=int(input())
max_=max(b)
count=0
for i in b:
if (max_==i):
count+=1
print(count)
|
Fibonacci numbers
1
2
3
4
5
6
| n=int(input())
b=[0,1]
for i in range(1,n+1,1):
b.append(b[i]+b[i-1])
print(b[len(b)-2])
|
The index of a Fibonacci number
Snakify作法(無unfind問題)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| a = int(input())
if a == 0:
print(0)
else:
fib_prev, fib_next = 0, 1
n = 1
while fib_next <= a:
if fib_next == a:
print(n)
break
fib_prev, fib_next = fib_next, fib_prev + fib_next
n += 1
else:
print(-1)
|
本人作法(有unfind問題)
1
2
3
4
5
6
7
8
9
10
11
12
| b=[0,1]
for i in range(1,100,1):
b.append(b[i]+b[i-1])
f=int(input())
flag=0
for i in range(0,100,1):
if (b[i]==f):
print(i)
flag=1
break
if (flag==0):
print(-1)
|
後記: 看完解答才想到可以用兩變數(fib1,fib2)的方式實現,既不會浪費太多記憶體也不會浪費太多時間
The maximum number of consecutive equal elements
1
2
3
4
5
6
7
8
9
10
11
12
13
| n=int(input())
pre=0
count=1
max_count=1
while n!=0:
if n==pre:
count+=1
max_count=max(max_count,count)
else:
count=1
pre=n
n=int(input())
print(max_count)
|
思路: 用count的方式跟最大數到的數字比,若比較大則取代之
CHPTER 7
:::success
List
1.單行輸入用列表生成式實現[ int(i) for i in input().split()]
2.多行輸入用append實現
3.切片技巧很方便
:::
Even indices
正常解
1
2
3
| a=input().split(" ")
for i in range(0,len(a),2):
print(a[i],end=" ")
|
一行流
1
| print(" ".join([i for i in input().split(' ')][0::2]))
|
Even elements
1
2
3
4
| a=[ int(i) for i in input().split()]
for i in a:
if (i%2==0):
print( i , end=' ')
|
後記: 列表生成式很好用 [ int(i) for i in input().split()]
可以直接生成一個列表
Greater than previous
1
2
3
4
5
6
7
| a=[int(e) for e in input().split()]
i=0
for _ in a:
if (i >= 1):
if (a[i]>a[i-1]):
print(a[i] ,end=" ")
i=i+1
|
Neighbors of the same sign
1
2
3
4
5
| a = [int(i) for i in input().split()]
for i in range(1,len(a),1):
if (a[i]*a[i-1]>0):
print(a[i-1],a[i],sep=" ",end=" ")
break
|
Greater than neighbours
1
2
3
4
5
6
| a = [int(i) for i in input().split()]
flag=0
for i in range(1,len(a)-1,1):
if (a[i]>a[i-1] and a[i]>a[i+1]):
flag+=1
print(flag)
|
The largest element
1
2
3
4
5
6
7
8
9
| a = [int(g) for g in input().split()]
max_ = a[0]
address = 0
for i in range(1,len(a),1):
if (a[i]>max_):
address = i
max_ = a[i]
print(max_ , address)
|
The number of distinct elements
1
2
3
4
5
6
| a=[i for i in input().split()]
count=0
for i in range (1,len(a),1):
if (a[i]!=a[i-1]):
count+=1
print(count+1)
|
Swap neighbours
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| a=[i for i in input().split()]
for i in range(0,len(a),2):
if (len(a)%2!=0):
if (i==len(a)-1):
break
tmp=a[i+1]
a[i+1]=a[i]
a[i]=tmp
else:
tmp=a[i+1]
a[i+1]=a[i]
a[i]=tmp
for i in range(0,len(a),1):
print(a[i],end=' ')
|
Swap min and max
1
2
3
4
5
6
7
8
| a=[int(e) for e in input().split()]
b=a.index(max(a))
c=a.index(min(a))
a[b],a[c]=a[c],a[b]
for i in a:
print(i,end=" ")
|
The number of pairs of equal
1
2
3
4
5
6
7
| a=[int(i) for i in input().split()]
count=0
for i in range (0,len(a),1):
for j in range(i+1,len(a),1):
if (a[i]==a[j]):
count+=1
print(count)
|
Unique elements
1
2
3
4
5
6
7
8
9
10
| a=[int(i) for i in input().split()]
flag=0
for i in range(0,len(a),1):
for j in range(0,len(a),1):
if (i != j):
if (a[i]==a[j]):
flag=1
if (flag==0):
print(a[i],end=" ")
flag=0
|
Queens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| a=[]
for i in range(8):
row_list=[int(g) for g in input().split()]
a.append(row_list)
flag=0
for i in range(8):
for g in range(8):
if(i==g):
break
elif ((abs(a[i][0]-a[g][0]))==(abs(a[i][1]-a[g][1]))):
flag=1
elif (a[i][0]==a[g][0]):
flag=1
elif (a[i][1]==a[g][1]):
flag=1
if (flag==1):
print("YES")
else:
print("NO")
|
後記: 個人認為本章最困難的一題,有偷偷用二維陣列實現,但也是可以用一維處理
The bowling alley
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| N,K=map(int,input().split())
a=[]
for i in range(K):
row_array=[int(g) for g in input().split()]
a.append(row_array)
pin=[1]*N
for i in range(K):
for j in range(a[i][0]-1,a[i][1],1):
pin[j]=0
for i in range(N):
if (pin[i]==0):
print(".",sep="",end="")
elif (pin[i]==1):
print("I",sep="",end="")
|
後記: 用bool的想法似乎又再多浪費時間,直接用.和I還比較快
CHAPTER 8
The length of the segment
1
2
3
4
5
6
7
8
9
10
11
12
| import math
def distance(a,b):
dis=math.sqrt(a**2+b**2)
return dis
x1=float(input())
y1=float(input())
x2=float(input())
y2=float(input())
print(distance(abs(x2-x1),abs(y2-y1)))
|
Negative exponent
1
2
3
| def power(a,b):
return a**b
print(power(float(input()),float(input())))
|
Uppercase
1
2
3
4
5
6
7
8
9
10
| def capitalize(a):
return chr(ord(a)-32)
a=input()
for i in range(0,len(a),1):
if(a[i-1]==' '):
print(capitalize(a[i]),end="")
elif (i==0):
print(capitalize(a[i]),end="")
else:
print(a[i],end="")
|
後記: ASCII表大寫小寫差32(小寫比較大)
:::warning
以下開始是遞迴
:::
Exponentiation
1
2
3
4
5
6
7
8
9
10
| def f(x,y):
if(y==1):
return x
elif (y==0):
return 1
else:
return f(x,y-1)*x
x=float(input())
y=float(input())
print(f(x,y))
|
Reverse the sequence
1
2
3
4
5
6
7
8
| def f():
a=int(input())
if (a!=0):
f()
# Isn't 0 it will keep input
print(a)
# when a=0 it will print
f()
|
Fibonacci numbers
1
2
3
4
5
6
7
8
| def fib(n):
if (n==1):
return 1
elif (n==0):
return 0
return fib(n-1)+fib(n-2)
n=int(input())
print(fib(n))
|
CHAPTER 9
Maximum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #Initialized & Input
a=[]
row,col=map(int,input().split())
for i in range(row):
c=[int(i) for i in input().split()]
a.append(c)
#Processing
max_=a[0][0]
address_x,address_y=0,0
for i in range(row):
for j in range(col):
if (a[i][j]>max_):
max_=a[i][j]
address_x,address_y=i,j
#Print
print(address_x,address_y)
|
Snowflake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #Initialized & Input
N=int(input())
a=[['.']*N for i in range(N)]
#Processing the list
for i in range(N):
for j in range(N):
if ( i==(N-1)/2 or j==(N-1)/2 ):
a[i][j] = "*"
elif (i == j):
a[i][j] = "*"
elif (i+j==(N-1)):
a[i][j] = "*"
#Print
for i in range(N):
for j in range(N):
print(a[i][j],end=" ",sep=" ")
print()
|
Chess board
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #Initialized & Input
row,col=[int(i) for i in input().split()]
a=[["."]*col for i in range(row)]
#Processing the list
for i in range(row):
for j in range(col):
if (i%2==1):
if (j%2==0):
a[i][j] = "*"
elif (i%2==0):
if (j%2==1):
a[i][j] = "*"
#Print
for i in range(row):
for j in range(col):
print(a[i][j],end=" ",sep=" ")
print()
|
注意: line 3 [["."]*col for i in range(row)]
不能寫成[["."]*col]*row
此種寫法的指標會和row=0時相同(原因是Python =和C++ = 概念不同)
The diagonal parallel to the main
1
2
3
4
5
| n = int(input())
for i in range(n):
for j in range(-i,n-i,1):
print (abs(j),end=" ",sep=" ")
print()
|
Side diagonal
1
2
3
4
5
6
7
8
9
10
11
12
13
| n=int(input())
a=[[0]*n for i in range(n)]
for i in range(n):
for j in range(n):
if (i+j==n-1):
a[i][j]=1
elif (i+j>n-1):
a[i][j]=2
for i in range(n):
for j in range(n):
print(a[i][j],sep=" ",end=" ")
print()
|
Swap the columns
1
2
3
4
5
6
7
8
9
10
11
12
13
| row,col=map(int,input().split())
a=[]
for i in range(row):
a.append([int(j) for j in input().split()])
change1,change2=map(int,input().split())
for i in range(row):
tmp=a[i][change2]
a[i][change2]=a[i][change1]
a[i][change1]=tmp
for i in range(row):
for j in range(col):
print(a[i][j],sep=" ",end=" ")
print()
|
Scale a matrix
1
2
3
4
5
6
7
8
9
10
| row,col=map(int,input().split())
a=[]
for i in range(row):
a.append([int(i) for i in input().split()])
product=int(input())
for i in range(row):
for j in range(col):
a[i][j]=a[i][j]*product
print(a[i][j],end=" ",sep=" ")
print()
|
Multiply two matrices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| m,n,r=map(int,input().split())
A=[]
B=[]
for i in range(m):
A.append([int(j) for j in input().split()])
for i in range(n):
B.append([int(j) for j in input().split()])
C=[[0]*r for i in range(m)]
for j in range(m):
for g in range(r):
for i in range(n):
C[j][g]=C[j][g]+A[j][i]*B[i][g]
for i in range(m):
for g in range(r):
print(C[i][g],end=" ")
print()
|
CHAPTER 10
The number of distinct numbers
1
2
3
4
5
6
7
| A={int(i) for i in input().split()}
max_=max(A)
count=0
for i in range(max_+1):
if (i in A):
count+=1
print(count)
|
The number of equal numbers
1
| print(len(set(input().split()).intersection(set(input().split()))))
|
The intersection of sets
1
| print(" ".join(sorted(set(input().split()).intersection(set(input().split())))))
|
Has the number been encountered before
1
2
3
4
5
6
| A=[int(i) for i in input().split()]
for i in range(len(A)):
if A[i] in A[0:i]:
print("YES")
else:
print("NO")
|
Cubes
1
2
3
4
5
6
7
8
9
10
11
12
13
| M,N=map(int,input().split())
Alice=set()
Bod=set()
for i in range(M):
Alice.add(input())
for i in range(N):
Bod.add(input())
print(len(Alice&Bod))
print("\n".join(sorted(Alice&Bod)))
print(len(Alice-Bod))
print("\n".join(sorted(Alice-Bod,key=lambda x:int(x))))
print(len(Bod-Alice))
print("\n".join(sorted(Bod-Alice)))
|
The number of distinct words in some text
1
2
3
4
5
6
7
8
| n=int(input())
sets=set()
for i in range(n):
line=input().split()
for j in line:
if j not in sets:
sets.add(j)
print(len(sets))
|
Guess the number
1
2
3
4
5
6
7
8
9
10
11
12
13
| all_={int(i) for i in range(1,int(input()))}
possible=all_
while True:
guess=input()
if guess=="HELP":
break
guess={int(i) for i in guess.split()}
ans=input()
if ans=="YES":
possible &= guess
elif ans=="NO":
possible &= (all_-guess)
print(" ".join([str(i) for i in possible]))
|
Polyglots
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| n=int(input())
language=[]
for i in range(int(input())):
language.append(input())
general=set(language)
all_=set(language)
for i in range(n-1):
language=[]
for j in range(int(input())):
language.append(input())
general&=set(language)
all_|=set(language)
general=sorted(list(general),key= lambda x:ord(x[0]))
all_=sorted(list(all_),key = lambda x:ord(x[0]))
print(len(general))
for i in general:
print(i)
print(len(all_))
for i in all_:
print(i)
|
CHAPTER 11
Number of occurrences
1
2
3
4
5
6
7
8
9
10
11
| a=input().split()
dic=dict()
for i in a:
if i not in dic:
print(0,end=" ")
dic[i]=1
else:
print(dic[i],end=" ")
dic[i]+=1
|
Dictionary of synonyms
1
2
3
4
5
6
7
8
9
10
11
12
| n=int(input())
dic=dict()
for i in range(n):
a=[j for j in input().split()]
dic[a[0]]=a[1]
word=input()
if word in dic:
print(dic[word])
else:
new_dic={v:k for k,v in dic.items()}
print(new_dic[word])
|
Elections in the USA
1
2
3
4
5
6
7
8
9
10
11
12
| n=int(input())
dic={}
for i in range(n):
a=[i for i in input().split()]
a[1]=int(a[1])
if a[0] not in dic:
dic[a[0]]=a[1]
elif a[0] in dic:
dic[a[0]]+=a[1]
new_dic=sorted(dic.items(),key= lambda x:(ord(x[0][0])))
for k,v in new_dic:
print(k,v)
|
The most frequent word
1
2
3
4
5
6
7
8
9
10
11
| n=int(input())
dic=dict()
for i in range(n):
a=input().split()
for j in a:
if j not in dic:
dic[j]=1
else:
dic[j]+=1
new=sorted(dic.items(),key= lambda x:(x[1],-ord(x[0][0])),reverse=True)
print(new[0][0])
|
Access rights
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| n=int(input())
access={}
for i in range(n):
program=input().split()
access[program[0]]=program[1:len(program)]
m=int(input())
for i in range(m):
program=input().split()
if "W" in access[program[1]] and program[0]=="write":
print("OK")
elif "R" in access[program[1]] and program[0]=="read":
print("OK")
elif "X" in access[program[1]] and program[0]=="execute":
print("OK")
else:
print("Access denied")
|
Countries and cities
1
2
3
4
5
6
7
8
9
10
11
12
| n=int(input())
dic={}
for i in range(n):
cities=input().split()
for city in cities[1:]:
dic[city]=cities[0]
m=int(input())
for i in range(m):
location=input()
print(dic[location])
|
Frequency analysis
1
2
3
4
5
6
7
8
9
10
11
12
13
| n=int(input())
dic={}
for i in range(n):
line=input().split()
for j in line:
if j not in dic:
dic[j]=1
else:
dic[j]+=1
new=[(-j,i) for (i,j) in dic.items()]
new=sorted(new)
for i,j in new:
print(j)
|
English-Latin dictionary
1
2
3
4
5
6
7
8
9
10
11
12
| dic={}
for i in range(int(input())):
line=input().split(" - ")
latin=line[1].split(", ")
for i in latin:
if i in dic:
dic[i].append(line[0])
else:
dic[i]=[line[0]]
print(len(dic))
for i in sorted(dic):
print(i,"-",", ".join(dic[i]))
|