Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Title 1: x to the data and y, k and return the least square method b
Method: Save the values of x and y is two arrays, and least-squares method to solve. The resulting b and the number of test cases less of a reserved and can not pass the test. . .
x = [1,2,3]
y = [3,5,6]
def fun(x, y):
size = len(x)
i = 0
sum_xy = 0
sum_y = 0
sum_x = 0
sum_suqre_x = 0
average_x = 0
average_y = 0
while i < size:
sum_xy += x[i] * y[i]
sum_y += y[i]
sum_x += x[i]
sum_suqre_x += x[i] *x[i]
i += 1
average_x = sum_x / size
average_y = sum_y / size
k = (size * sum_xy - sum_x * sum_y) / (size* sum_suqre_x-sum_x*sum_x)
b = average_y - average_x*k
print(len(str(b)))
return k , b
print(fun(x,y))
Problem 2: for N and R, array [l, 2,3 ..., N] of the whole arrangement, statistical bubble sort requires only the number of array R times, returns the number of array
Method: the whole arrangement configuration, if R satisfies 1 times, if it exceeds the break return. But ultimately, AC10%, exceeded the memory of the display section
n = 3
r = 1
def permute(nums):
res = []
def back(nums, tmp):
if not nums:
print(tmp)
if count(tmp) == r:
print(tmp)
return
for i in range(len(nums)):
back(nums[:i] + nums[i+1:], tmp + [nums[i]])
back(nums, [])
return res
dp = permute([i for i in range(1,n+1)])
print(dp)
def count(a):
count = -1
for i in range(len(a)):
count += 1
flag = True
for j in range(len(a)-i-1):
if a[j] > a[j+1]:
a[j], a[j+1] = a[j+1], a[j]
flag = False
if flag or count > r:
break
return count