iT邦幫忙

2024 iThome 鐵人賽

DAY 15
0
Python

一起來用 Snakify 練練手系列 第 15

【一起來用 Snakify 練練手】Day15 Lesson 6 練習題參考解答

  • 分享至 

  • xImage
  •  

我個人認為從這一個單元開始,就需要多動一點腦袋

  • List of squares
    😈 基本題,用迴圈跑而已
a = int(input())
i = 1
while(i**2<=a):
    print(i**2)
    i += 1
  • Least divisor(最小除數)
    😈 你可以從2開始檢查,逐一檢查這些數字是否能整除 n。
number = int(input())
if number == 2:
    print(number)
else:
    if (number % 2 == 0):
        i = 2
        while i <= number:
            if number % i == 0 :
                print(i)
                break
            i+=2
    else:
        i = 3
        while i <= number:
            if number % i == 0 :
                print(i)
                break
            i+=2
  • The power of two
    😈 用一個變數去紀錄是第幾個數即可
a = int(input())
i = 1
while(2**i <= a):
    i += 1
print(i-1)
print(2**(i-1))
  • Morning jog
    😈 數學計算
x = int(input())
y = int(input())
i = 1

while (x < y):
    x += 0.1*x
    i+=1
    
print(i)
  • The length of the sequence
    😈 計算長度
l = 0
while True:
    i = int(input())
    if i == 0:
        break
    else:
        l += 1
print(l)
  • The sum of the sequence
    😈 數列加總
s = 0
while True:
    i = int(input())
    if i == 0:
        break
    else:
        s += i
print(s)
  • The average of the sequence
    😈 求平均
s = 0
l = 0
while True:
    i = int(input())
    if i == 0:
        break
    else:
        s += i
    l += 1
print(s/l)
  • List of squares
    😈 找最大值
s = 0
b = 0
while True:
    i = int(input())
    if i == 0:
        break
    else:
        if i > b:
            b = i
print(b)
  • The index of the maximum of a sequence
    😈 最大值的位置
s = 0
b = 0
l = 0
a = 0
while True:
    i = int(input())
    if i == 0:
        break
    else:
        if i > b:
            b = i
            a = l
    l += 1
print(a+1)
  • The number of even elements of the sequence
    😈 偶數有幾個
i = 0
a = 0
while True:
    i = int(input())
    if i == 0:
        break
    elif i%2 == 0:
        a += 1
    
print(a)
  • The number of elements that are greater than the previous one
    😈 大於前一個元數有幾個
i = 0
a = 0
c = 0
while True:
    i = int(input())
    if i == 0:
        break
    elif a < i :
        c += 1
    a = i
    
print(c-1)
  • The second maximum
    😈 第二大的數字
f = int(input())
s= int(input())
if f < s:
    f, s = s, f
i = int(input())
while i != 0:
    if i > f:
        s, f = f, i
    elif i > s:
        s = i
    i = int(input())
print(s)
  • The number of elements equal to the maximum
    😈 最大數有幾個
s = 0
b = 0
c = 0
while True:
    i = int(input())
    if i == 0:
        break
    else:
        if i >= b:
            if i == b:
                c += 1
            else:
                b = i
                c = 0

print(c+1)
  • Fibonacci numbers
    😈 經典問題,一定要懂
i = int(input())
if i == 0:
    print(0)
else:
    p, n = 0, 1
    j = 2
    while (j<=i):
        p, n = n, n+p
        j += 1
    print(n)     
  • The index of a Fibonacci number
    😈 取索引值
a = int(input())
flag = 0

b = [0, 1]
for i in range(1,100,1):
    b.append(b[i] + b[i-1])

for i in range(0, 100, 1):
    if (b[i] == a):
        print(i)
        flag = 1
        break
if (flag == 0):
    print(-1)
  • The maximum number of consecutive equal elements
    😈 最長的連續相等元素片段的長度
p = 0
a = 1
m = 1
while True:
    i = int(input())
    if i ==0:
        break
    elif i == p:
        a += 1
        m = max(a, m)
    else:
        a = 1
    p = i
print(m)

上一篇
【一起來用 Snakify 練練手】Day14 Lesson 6 概念
下一篇
【一起來用 Snakify 練練手】Day16 Lesson 7 概念
系列文
一起來用 Snakify 練練手30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言