close

 501 訊息顯示

說明:

請撰寫一程式,呼叫函式compute(),該函式功能為讓使用者輸入系別(Department)、學號(StudentID)和姓名(Name)並顯示這些訊息。

範例輸入:

EE

b171532

cc

輸入/輸出

image

 

Code:

def compute(a, b,c):
  print(f"Department:{a}")
  print(f"Student ID:{b}")
  print(f"Name:{c}")

a =input()
b =input()
c=input()
compute(a,b,c)

 

 

502 乘積

說明:

請撰寫一程式,將使用者輸入的兩個整數作為參數傳遞給一個名為compute(x, y)的函式,此函式將回傳x和y的乘積。

輸入/輸出

範例輸入:

77

9

範例輸出:

image

Code:

 

def compute(a, b):
  return a*b


a = int(input())
b = int(input())
print(compute(a, b))

 

503 連加計算

說明:

請撰寫一程式,讓使用者輸入兩個整數,接著呼叫函式compute(),此函式接收兩個參數a、b,並回傳從a連加到b的和。

輸入/輸出

範例輸入:

1

17

範例輸出:

 

image

 

Code:

def compute(a, b):
  tmp=0
  for i in range(a,b+1):
    tmp+=i
  return tmp


a = int(input())
b = int(input())
print(compute(a, b))

 

 

504 次方計算

 

說明:

請撰寫一程式,讓使用者輸入兩個整數,接著呼叫函式compute(),此函式接收兩個參數a、b,並回傳ab的值。

輸入/輸出

範例輸入:

7

7

範例輸出:

image

Code:

def compute(a,b):
    return a**b
a=int(input())
b=int(input())
print(compute(a,b))

505 依參數格式化輸出

說明:

請撰寫一程式,將使用者輸入的三個參數,變數名稱分別為a(代表字元character)、x(代表個數)、y(代表列數),作為參數傳遞給一個名為compute()的函式,該函式功能為:一列印出x個a字元,總共印出y列。

輸出的每一個字元後方有一空格。

輸入/輸出

範例輸入:

@

7

5

範例輸出:

image

Code:

 

def compute(a,b,c):
    for j in range(c):
        for i in range(b):
            print(a+" ",end="")
        print()
a=input()
b=int(input())
c=int(input())
compute(a,b,c)

506 一元二次方程式

說明:

請撰寫一程式,將使用者輸入的三個整數(代表一元二次方程式 ax2+bx+c=0的三個係數a、b、c)作為參數傳遞給一個名為compute()的函式,該函式回傳方程式的解,如無解則輸出【Your equation has noroot.】

輸出有順序性

輸入/輸出

範例輸入:

2

3

1

範例輸出:

image

 

Code:

def compute(x,y,z):
    if (y**2)-(4*x*z)<0:
        print("Your equation has noroot.")
    else:
        ans1=(-y+(y**2-4*(x*z))**0.5)/2*x
        ans2=(-y-(y**2-4*(x*z))**0.5)/2*x
        return  str(ans1)+","+str(ans2)
a=eval(input())
b=eval(input())
c=eval(input())
print(compute(a,b,c))

507 質數

說明:

請撰寫一程式,讓使用者輸入一個整數x,並將x傳遞給名為compute()的函式,此函式將回傳x是否為質數(Prime number)的布林值,接著再將判斷結果輸出。如輸入值為質數顯示【Prime】,否則顯示【Not Prime】。

輸入/輸出

範例輸入:

7511

範例輸出:

image

Code:

def compute(x):
    if x<=1:
        return "Not Prime"
    for i in range(2,x):
        if x%i==0:
            return "Not Prime"
    return "Prime"
x=int(input())
print(compute(x))

508 最大公因數

說明:

請撰寫一程式,讓使用者輸入兩個正整數x、y,並將x與y傳遞給名為compute()的函式,此函式回傳x和y的最大公因數。

輸入/輸出

範例輸入:

79

87

範例輸出:

image

Code:

 

def compute(x,y):
    r=x%y
    while r!=0:
        x=y
        y=r
        r=x%y
    return y
x=eval(input())
y=eval(input())
x,y=max(x,y),min(x,y)
print(compute(x,y))

509 最簡分數

說明:

請撰寫一程式,讓使用者輸入二個分數,分別是x/y和m/n(其中x、y、m、n皆為正整數),計算這兩個分數的和為p/q,接著將p和q傳遞給名為compute()函式,此函式回傳p和q的最大公因數(Greatest Common Divisor, GCD)。再將p和q各除以其最大公因數,最後輸出的結果必須以最簡分數表示。

輸入/輸出

1

3

2

7

範例輸出:

image

Code:

 

def  compute(x,y):
    gcd = 1
    for i in range(1,min(x,y)+1):
        if x%i==0 and y%i==0:
            gcd=i
    return gcd

x,y = eval(input())
m,n = eval(input())
q = y*n
p = m*y+x*n
gcd=compute(p,q)

print(x,'/',y,' + ',m,'/',n,' = ',int(p/gcd),'/',int(q/gcd))

510 費氏數列

說明:

請撰寫一程式,計算費氏數列(Fibonacci numbers),使用者輸入一正整數num (num>=2),並將它傳遞給名為compute()的函式,此函式將輸出費氏數列前num個的數值。

費氏數列的某一項數字是其前兩項的和,而且第0項為0,第一項為1,表示方式如下:

輸入/輸出

範例輸入:

14

範例輸出:

image

 

Code:

def compute(n):
    if n==0 or n==1:
        return n
    else:
        return compute(n-1)+compute(n-2)
x=eval(input())
for i in range(x):
    print(compute(i),end=" ")

系列文章:

101-110

201-210

301-310

401-410

501-510

601-610

701-710

801-810

901-910


arrow
arrow
    文章標籤
    TQC python TQC+
    全站熱搜

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()