close
Input
輸入的第一列有一個整數 T (1≦T≦100),代表以下有多少組測試資料。

每組測試資料為兩列,包含兩個數 a 與 b (0≦a≦b≦1000)。
Output
每組測試資料輸出一列,內容為 a 及 b 間所有完全平方數的和。
Sample Input #1
2
1
5
5
35
Sample Output #1
Case 1: 5


Case 2: 50

 

python 解法一
#解法一:把之間的數每個開平方後相成,若開平方後相成
#後還是一樣。就是完全平方數
bear=int(input())
for case in range(bear):
    a=int(input())
    b=int(input())
    ans=0
    for j in range(a,b):
        if int(j**0.5)*int(j**0.5)==j:
            ans+=j
        #想法
    print("Case",case+1,end="")
    print(":",ans)

 

python 解法二:

#解法二:先建表
#然後從裡面抓需要的數
from sys import stdin
arr=[i**2 for i in range(35)]
num=int(stdin.readline())
for i in range(num):
    a=int(stdin.readline())
    b=int(stdin.readline())
    arr1=sum([s for s in arr if s>=a and s<=b])
    print(f'Case {i+1}: {arr1}')
arrow
arrow
    文章標籤
    python 高中生程式解題
    全站熱搜
    創作者介紹
    創作者 趴趴熊日常 的頭像
    趴趴熊日常

    資工趴趴熊的小天地

    趴趴熊日常 發表在 痞客邦 留言(0) 人氣()