博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python CG练习题
阅读量:801 次
发布时间:2019-03-26

本文共 6188 字,大约阅读时间需要 20 分钟。

输入方式

a, b, c = map(float, input().split())
alist = list(map(int, input().split()))
n = int(input())
#数组
for i in range(n):     Y[i] =  int( input() )
import numpyA=numpy.array(list(map(int, input().split())))
s = str.split() arr = np.array(s, dtype=int)
#二维数组 arr = [[0] * n] * n for i in range(n):
arr[i] = input().split(" ")     arr[i] = [int(j) for j in arr[i]]
# 创建一个 5x3 的数组 x1 = np.arange(15).reshape((5, 3)) x1[2][2]= 999 print(x1)
 
#
 

递归-全排列

import numpydef Output(alist):    res = numpy.array(alist)    print(res)def PERM(alist,sta,end):    if sta==end: Output(alist)    else:        for i in range(sta,end):            alist[sta],alist[i] = alist[i] ,alist[sta]            PERM(alist,sta+1,end)            alist[sta], alist[i] = alist[i],alist[sta]alist = list(map(int, input().split()))PERM(alist,0,len(alist))

迭代法求函数的根

import numpy as npdef cal(k):    return k**5 - k*2 - 1def Find( n ):   Y = np.linspace(alist[0], alist[1], 9)   # print(Y)   for i in range(1,9):        if cal( Y[i-1] )*cal(Y[i]) <0:            # print(Y[i],Y[i-1],""choose",(Y[i]+Y[i-1])/2)            Y0.append((Y[i]+Y[i-1])/2  )        if i<=7 and  ( ( cal( Y[i+1] ) - cal( Y[i] ) )* ( cal( Y[i] ) - cal(Y[i-1]) ) )  < 0  and abs( cal(Y[i]) ) < 0.1:            # print("find", Y[i])            Y0.append(Y[i])def g1(x):    while abs( (pow( (x*2+1) ,0.2) if(x*2+1>=0) else -pow( -(x*2+1) ,0.2) ) - x ) > delta :        x =  pow( (x*2+1) ,0.2) if(x*2+1>=0) else -pow( -(x*2+1) ,0.2)    x = pow( (x*2+1) ,0.2) if(x*2+1>=0) else -pow( -(x*2+1) ,0.2)    return xdef g2(x):    while abs( (pow(x,5)-1)/2-x ) > delta :        x = (pow(x,5)-1)/2    x = (pow(x,5)-1)/2    return xdef run(x):    if x <-0.7 or x >0:        res = g1(x)    else : res = g2(x)    # print("答案是",res)    return resif __name__ == "__main__":    # print("HI")    alist = list(map(float,input().split()))    delta = 10**(-int(alist[2]))    Y0 = list()    Find(8)    Y0.sort()    # print(Y0)    for i in range(3):        # print("Y0 is ",Y0[i])        print( "%.3f" %run( Y0[i] ) )

试值法求根

def cal(x):    return 12 * 300 * ((1 + x / 12) ** 240 - 1) / x - 500000def MID(x, y):    return y - cal(y) * (y - x) / (cal(y) - cal(x))a, b, c = map(float, input().split())c = int(c)cnt = 0res = awhile b - a > pow(10, -c) / 2:    mid = MID(a, b)    if abs(cal(mid)) < 0.0001: break    if (cal(mid) <= 0):        a = mid    else:        b = mid    cnt = cnt + 1print(cnt)print(round(MID(a, b), c))

牛顿法求根

ee = 2.718281828459045235360287471def f(x):    return 9600*( 1 - ee**( -x/15 ) ) - 480 * x# y=f(t)=9600*(1-e**(-t/15.0)) - 480*t;x=r(t)=2400*(1-e**(-t/15.0))def f_(x):    return 9600/15* ( ee**(-x/15) ) - 480def r(x):    return 2400*( 1 - ee**( -x/15 ) )def Cal(a):    return a - f(a)/f_(a)if __name__ =="__main__":    a,b,c = map( float,input().split( ) )    # print(a,b,c)    b = 10**(-int(b))    c = 10**(-int(c))    # cnt = 0    while( abs( a-Cal(a) ) > b or abs(   f( Cal(a) ) ) > c ):        a = Cal(a)        # cnt = cnt + 1        # print(cnt,a,abs( a-Cal(a) ) ,abs( f( Cal(a) ) ) ,b,c)    print("%.5f"% a)    print("%.5f" % r(a))    '''    输出错误  错误输出:8 1 1期望输出:9.089551090.69211测试数据2    输出错误  错误输出:8 3 3期望输出:9.087901090.54798    '''

快速排序

import numpyimport mathdef find( Thelist,s,e ):    if( s == e ): return s    Mid = Thelist[e]    MinId = s    for i in range(s,e):        if( Thelist[i]<=Mid ):            Thelist[i],Thelist[MinId] = Thelist[MinId],Thelist[i]            MinId = MinId+1    Thelist[e], Thelist[MinId] = Thelist[MinId], Thelist[e]    print(Mid,MinId)    return MinIddef QuickSort( TheList,s,e ):    if( s

二分找点

def find(l,r):    mid = int( (l+r)/2 )    if( l>r ) :return int(-1)    if( A[mid]== n): return mid    else:        if( A[mid]

归并排序

import numpyimport mathdef Merge(Llist,Rlist):    res = []    Lpos,Rpos,pos = 0,0,0    while Lpos

 二分

def cal(x):    return 12  * 300  * ((1 + x / 12) ** 240 - 1)/ x - 500000def MID(x,y):    return x/2+y/2    # return y - cal(y)*(y-x)/( cal(y)-cal(x) )a,b,c = map(float,input().split())c = int(c)cnt = 0while b-a > pow(10, -c):    mid = MID(a,b)    cnt = cnt + 1    if( cal(mid)>=0 ) :  b = mid    else: a = midprint(cnt)print( round(MID(a,b),c) )

上三角线性方程

import numpy as npif __name__ == "__main__":    n = int(input())    arr = [[0] * n] * n    for i in range(n):        arr[i] = input().split(" ")        arr[i] = [int(j) for j in arr[i]]    Y = [0]*n    res = [0.0]*n    for i in range(n):        Y[i] =  int( input() )    i = int(n-1)    res[i] = Y[i]/arr[n-1][n-1]    i = i - 1    while i>=0:        sum = int(0)        for j in range(i+1,n):            sum = sum + arr[i][j]*res[j]        res[i] = ( Y[i]-sum )/arr[i][i]        i = i-1    ans = np.array(res).reshape(n,1)    print(ans)

select算法

import mathflag = 1def PARTITION(alist,l,r,x):    pos  = l    posx = l    for i in range(l,r+1):        if alist[i] <= x:            if( alist[i]==x ):                posx = i            alist[pos],alist[i] = alist[i],alist[pos]            pos = pos + 1    alist[pos-1],alist[posx] = alist[posx],alist[pos-1]    return max(pos-1,l)def SELECT( alist,l,r,n ):    global flag    Len = r - l + 1    if Len <= 10:        alist[l:r+1] = sorted( alist[l:r+1] )        return alist[  max(int(l+n-1),l)]    #分组递归求中位数的中位数,其实就是找基准的过程    Groups = Len//5  #这么多组    L = l    TemList = []    for i in range(Groups-1):        TemList.append( SELECT( alist,L,L+4,3 ) )        L = L+5    TemList.append( SELECT(alist, L, r, (r-L+2)//2 ) )    x = SELECT(TemList , 0 , Groups-1 ,(Groups+1)//2 )  #分组后的中位数取第(num_group/2向上取整)小的元素。    if flag == 1 :        print(x)        flag = 0    #以x为基准寻找小于基准的下标位置q    q = PARTITION( alist,l,r,x)    nums = q-l+1    if nums == n:        return x    elif nums>n:        return SELECT(alist,l,q-1,n)    return int(SELECT(alist,q+1,r,n-nums))if __name__ == "__main__":    alist = list( map(int,input().split()) )    n = int( input() )    # print(alist,n)    print( SELECT( alist,int(0),int(len(alist)-1),int(n) ) )'''测试数据1    输出错误  错误输出:2 9 8 0 7 10 1 12 3 14 5 13 6 11 43期望输出:72测试数据2    输出错误  错误输出: 32 22 28 11 24  8  9 12 10 19 36 16 39 50 14 30 21 23  3 43 46 35 17 31 18 42 44 34 27 33 15 45 29  5 13 38 26  6  0  1 47 40 41 25  7 20  2  4  37 49 4820期望输出:2319'''

转载地址:http://dvmyk.baihongyu.com/

你可能感兴趣的文章