python Exercise 5

Given two does not exceed positive integers a and n 9, requiring the preparation of the function fn (a, n) find a + aa + aaa ++ ⋯ + aa ⋯ aa (n th a) the sum, fn to be returned is the number of columns and

Function interface definition:


fn(a,n)
其中 a 和 n 都是用户传入的参数。 a 的值在[1, 9]范围;n 是[1, 9]区间内的个位数。函数须返回级数和
 

Referee test program Example:


/* 请在这里填写答案 */
		 
a,b=input().split()
s=fn(int(a),int(b))
print(s)
 

Sample input:

Here we are given a set of inputs. E.g:

2 3
 

Sample output:

Given here corresponding output. E.g:

246

 

def fn(a, b):
    # 求和
    sum = 0
    for i in range(0,b):
        sa = 0
        for n in range(0,i+1):
            sa = sa + a*(10**n)
        sum = sum + sa
        # print("第%d次为%d"%(i+1,sum))
    return sum

 

 

Use cosine function approximation function evaluation

 

This problem required to achieve a function, seeking to cos (x) using the following approximation formula, the nearest (eps is not less than the absolute value of items added) is smaller than the absolute value of the last one of eps:

cos (x) = x^0 / 0! - x^2 / 2! + x^4 / 4! - x^6 / 6! + ?

Function interface definition: funcos (eps, x), where the user is passed in parameter and eps x; funcos function should return it with a given formula, retain decimal four.

Function interface definition:

函数接口:
funcos(eps,x ),返回cos(x)的值。
 

Referee test program Example:

在这里给出函数被调用进行测试的例子。例如:


/* 请在这里填写答案 */

eps=float(input())
x=float(input())
value=funcos(eps,x )
print("cos({0}) = {1:.4f}".format(x,value))
 

Sample input:

Here we are given a set of inputs. E.g:

0.0001
-3.1
 

Sample output:

Given here corresponding output. E.g:

cos(-3.1) = -0.9991
DEF funcos (EPS, X): 
    SUM = 0.0000
     for I in Range (0,10 ):
         # Print (I) 
        Y = 2 * I
         # Print (Y) 
        # factorial 
        Jiecheng. 1 = # Initialization IF (Y == 0 ):
             # factorial is 0. 1 
            Jiecheng. 1 = the else :
             for n- in Range (. 1,. 1 + Y ): 
                Jiecheng = Jiecheng * n-
         # Print (Jiecheng) # Print (X ** Y / Jiecheng)
        
        
        
        
        = X ** Y value / Jiecheng
         # Print ( "% d of times for .4f%"% (I +. 1, value)) 
        IF (ABS (value) < EPS):
             BREAK 
        # summing 
        IF (I = 2% = 0):
             # even number + 
            SUM = SUM + value
         the else :
             # radix - 
            SUM = SUM - value
         # Print (SUM) 
    return SUM

 

Guess you like

Origin www.cnblogs.com/msdog/p/12638913.html