输入方式
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 numpy
A=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 numpy
def 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 np
def cal(k):
return k**5 - k*2 - 1
def 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 x
def g2(x):
while abs( (pow(x,5)-1)/2-x ) > delta :
x = (pow(x,5)-1)/2
x = (pow(x,5)-1)/2
return x
def run(x):
if x <-0.7 or x >0:
res = g1(x)
else : res = g2(x)
# print("答案是",res)
return res
if __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 - 500000
def MID(x, y):
return y - cal(y) * (y - x) / (cal(y) - cal(x))
a, b, c = map(float, input().split())
c = int(c)
cnt = 0
res = a
while 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 + 1
print(cnt)
print(round(MID(a, b), c))
牛顿法求根
ee = 2.718281828459045235360287471
def 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) ) - 480
def 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.08955
1090.69211
测试数据2 输出错误
错误输出:
8 3 3
期望输出:
9.08790
1090.54798
'''
快速排序
import numpy
import math
def 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 MinId
def QuickSort( TheList,s,e ):
if( s<e ):
pos = find(TheList,s,e)
QuickSort(TheList,s,pos-1)
QuickSort(TheList,pos+1,e)
alist = list(map(int,input().split()))
LEN = len(alist)
QuickSort(alist,0,LEN-1)
print( numpy.array( alist ))
'''
【样例输入】
2 8 7 1 3 5 6 4
【样例输出】
4 3
3 2
1 0
8 7
6 5
[1 2 3 4 5 6 7 8]
'''
二分找点
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]<n ): return find( mid+1,r )
return find(l,mid-1)
A = list(map(int, input().split()))
n = int(input())
print( find(0,len(A)-1)+1 )
归并排序
import numpy
import math
def Merge(Llist,Rlist):
res = []
Lpos,Rpos,pos = 0,0,0
while Lpos<len(Llist) and Rpos<len(Rlist) :
if( Llist[Lpos] < Rlist[Rpos] ):
res.append(Llist[Lpos])
Lpos= Lpos + 1
else:
res.append(Rlist[Rpos])
Rpos = Rpos + 1
pos = pos + 1
res += list( Llist[Lpos:] )
res += list( Rlist[Rpos:] )
return res
def MergeSort( L ):
print(numpy.array(L))
if( len(L)<=1 ) : return L
mid = math.ceil( len(L)/2 )
return Merge( MergeSort( L[:mid] ), MergeSort( L[mid:] ) )
alist = list(map(int,input().split()))
print( numpy.array(MergeSort(alist)))
二分
def cal(x):
return 12 * 300 * ((1 + x / 12) ** 240 - 1)/ x - 500000
def 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 = 0
while b-a > pow(10, -c):
mid = MID(a,b)
cnt = cnt + 1
if( cal(mid)>=0 ) : b = mid
else: a = mid
print(cnt)
print( round(MID(a,b),c) )
上三角线性方程
import numpy as np
if __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 math
flag = 1
def 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 4
3
期望输出:
7
2
测试数据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 48
20
期望输出:
23
19
'''