6-1 6-1.使用函數求特殊a串數列和 (30 分)
給定兩個均不超過9的正整數a和n,要求編寫函數fn(a,n) 求a+aa+aaa++⋯+aa⋯aa(n個a)之和,fn須返回的是數列和
函數接口定義:
fn(a,n)
其中 a 和 n 都是用戶傳入的參數。 a 的值在[1, 9]范圍;n 是[1, 9]區間內的個位數。函數須返回級數和
裁判測試程序樣例:
/* 請在這里填寫答案 */
a,b=input().split()
s=fn(int(a),int(b))
print(s)
輸入樣例:
在這里給出一組輸入。例如:
2 3
輸出樣例:
在這里給出相應的輸出。例如:
246
def fn(x,y): item = 0 sum = 0 for i in range(y): item = item*10+x sum += item return sum a,b=input().split() sum=fn(int(a),int(b)) print(sum) exit(0)
6-2 6-5.使用函數求余弦函數的近似值 (20 分)
本題要求實現一個函數,用下列公式求cos(x)近似值,精確到最后一項的絕對值小於eps(絕對值小於eps的項不要加):
cos (x) = x^0 / 0! - x^2 / 2! + x^4 / 4! - x^6 / 6! + ?
函數接口定義:funcos(eps,x ),其中用戶傳入的參數為eps和x;函數funcos應返回用給定公式計算出來,保留小數4位。
函數接口定義:
函數接口:
funcos(eps,x ),返回cos(x)的值。
裁判測試程序樣例:
在這里給出函數被調用進行測試的例子。例如:
/* 請在這里填寫答案 */
eps=float(input())
x=float(input())
value=funcos(eps,x )
print("cos({0}) = {1:.4f}".format(x,value))
輸入樣例:
在這里給出一組輸入。例如:
0.0001
-3.1
輸出樣例:
在這里給出相應的輸出。例如:
cos(-3.1) = -0.9991
def factorial(n): if n==0: return 1 sum=n*factorial(n-1) return sum def funcos(eps,x): s=0 i=0 while x**i/factorial(i)>eps or x**i/factorial(i)==eps: i=i+2 for j in range(0,i,2): if j%4==0: s=s+x**j/factorial(j) else: s=s-x**j/factorial(j) print('cos({0}) = {1:.4f}'.format(x,s))