来自杭州电子科技大学自动化专业的大二理工男一枚。
一只整个暑假都在学校学习的学生。
准备更新资料:无
b站同性交友帐号搜索:承包户
steam好友代码:241282993
喜欢的游戏:明日方舟、csgo、彩虹六号……
如果想联系我也可以加QQ:2114496089
加好友都请备注来意。
Personal Blog
来自杭州电子科技大学自动化专业的大二理工男一枚。
一只整个暑假都在学校学习的学生。
准备更新资料:无
b站同性交友帐号搜索:承包户
steam好友代码:241282993
喜欢的游戏:明日方舟、csgo、彩虹六号……
如果想联系我也可以加QQ:2114496089
加好友都请备注来意。
服务器主要功能为个人博客,于2021.7.1开始正式运营,为wordpress模板。
该博客会不定期更新个人学习到的知识进行分享,你也可以通过我的博客了解我。
请不要在此网站泄露任何个人信息,评论姓名等个人信息请匿名填写,个人不能保证此网站不被攻击,如果出问题请自行负责!请爱护服务器,大佬请不要无故攻击谢谢!请不要对服务器运行爬虫或各种压力测试软件,谢谢,服务器设置每秒访问上限,可能你会被封ip。服务器运行速度及网速可能稍慢,望见谅(有钱了买个好的)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
class doubleIntegral: def __init__(self, xMin, xMax, yMin, yMax, func): self.xMin = xMin self.xMax = xMax self.yMin = yMin self.yMax = yMax self.func = func def trapezoid(self, nX, nY): result = 0 betweenX = (self.xMax - self.xMin) / nX betweenY = (self.yMax - self.yMin) / nY sums = [] for i in range(nY + 1): temp = 0 for j in range(nX): temp += betweenX / 2 * (self.func(self.xMin + j * betweenX, self.yMin + i * betweenY) + self.func(self.xMin + (j + 1) * betweenX, self.yMin + i * betweenY)) sums.append(temp) ans = 0 for i in range(nY): ans += betweenY / 2 * (sums[i] + sums[i + 1]) return ans def simpsons(self, nX, nY): result = 0 betweenX = (self.xMax - self.xMin) / nX betweenY = (self.yMax - self.yMin) / nY / 2 sums = [] for i in range(2 * nY + 1): temp = 0 for j in range(nX): a = self.xMin + j * betweenX b = self.xMin + (j + 1) * betweenX c = (a + b) / 2 temp += betweenX / 6 * (self.func(a, self.yMin + i * betweenY) + self.func( b, self.yMin + i * betweenY) + 4 * self.func(c, self.yMin + i * betweenY)) sums.append(temp) ans = 0 for i in range(nY): ans += betweenY / 3 * \ (sums[i * 2] + sums[(i + 1) * 2] + 4 * sums[i * 2 + 1]) return ans if __name__ == "__main__": # 个人不知道为什么梯形法求出来不对,有兴趣的可以自己去看看 # 输入格式:xMin, xMax, yMin, yMax, func # 输入格式': nx, ny print(doubleIntegral(0, 1, 0, 2, lambda x, y: x**2+2*y).trapezoid(4, 8)) print(doubleIntegral(0, 1, 0, 2, lambda x, y: x**2+2*y).simpsons(4, 8)) |
树莓派 frpc 设置:
从 github 中找到 arm 版本 wget 下载
frpc.ini 参考配置
1 2 3 4 5 6 7 8 9 10 |
[common] server_addr = you server ip server_port = 7000 token = your password [jupyter] type = http local_ip = 127.0.0.1 local_port = 8888 custom_domains = your domain |
jupyter 设置跟之前文章相同,即 ip, allow_remote_access等设置
服务器端 frps 配置: 云端开启7000~7003端口
1 2 3 4 5 6 7 8 |
[common] bind_port = 7000 vhost_http_port = 7002 token = your password dashboard_port = 7001 dashboard_user = Your dashboard usr name dashboard_pwd = Your dashboard password vhost_http_port = 7003 |
云端 nginx 参考配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
server{ listen 80; listen [::]:80; server_name yourdomain; location / { proxy_pass http://127.0.0.1:7003; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 120s; proxy_next_upstream error; } } |
最终效果即通过域名 python.pancake2021.work 访问树莓派 jupyter
全部是正确答案,慎用。Adams和Milne需要用另外的算法如runge-kutta(代码示例求法)求前3个数,当然也可以用Euler和Euler Improved,大家不要都一样啦。Python缩进规范请注意!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def solveByEulerImproved(f, epsilon, a, y_a, b): function = Result.get_function(f) n = 1 delta = float("inf") while abs(delta) >= epsilon / 10: n *= 2 h = (b - a) / n pre_y = y_a pre_fy = function(a, y_a) for i in range(n): x = a + (i + 1 / 2) * h yHalf = pre_y + pre_fy * h / 2 fyHalf = function(x, yHalf) y = pre_y + h * fyHalf delta = pre_y - y pre_y = y pre_fy = function(x + h / 2, y) return y |
Euler method 理论上代码正确,但是实际上因为算法的原因,与测试样例的值偏差会过大,要通过测试样例只需要初始化时把 n = 10000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def solveByEuler(f, epsilon, a, y_a, b): function = Result.get_function(f) n = 1 delta = float("inf") while abs(delta) >= epsilon / 10: n *= 2 h = (b - a) / n pre_y = y_a for i in range(n): x = a + i * h fy = function(x, pre_y) y = pre_y + h * fy delta = pre_y - y pre_y = y return y |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def solveByRungeKutta(f, epsilon, a, y_a, b): function = Result.get_function(f) n = 1 delta = float("inf") while abs(delta) >= epsilon / 10: n *= 2 h = (b - a) / n pre_y = y_a for i in range(n): x = a + i * h k1 = h * function(x, pre_y) k2 = h * function(x + h / 2, pre_y + k1 / 2) k3 = h * function(x + h / 2, pre_y + k2 / 2) k4 = h * function(x + h, pre_y + k3) y = (k1 + 2 * k2 + 2 * k3 + k4) / 6 + pre_y delta = pre_y - y pre_y = y return y |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
def runge_kutta(h, a, y_a, function): y_dot = [function(a, y_a)] pre_y = y_a for i in range(3): x = a + i * h k1 = h * function(x, pre_y) k2 = h * function(x + h / 2, pre_y + k1 / 2) k3 = h * function(x + h / 2, pre_y + k2 / 2) k4 = h * function(x + h, pre_y + k3) y = (k1 + 2 * k2 + 2 * k3 + k4) / 6 + pre_y y_dot.append(function(x + h, y)) pre_y = y return y, y_dot def solveByAdams(f, epsilon, a, y_a, b): function = Result.get_function(f) n = 2 delta = float("inf") while abs(delta) >= epsilon / 10: n *= 2 h = (b - a) / n y, y_dot = Result.runge_kutta(h, a, y_a, function) for i in range(n - 3): delta = (55 * y_dot[-1] - 59 * y_dot[-2] + 37 * y_dot[-3] - 9 * y_dot[-4]) / 24 * h y += delta y_dot.append(function(a + (i + 4) * h, y)) return y |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
def runge_kutta(h, a, y_a, function): y_dot = [function(a, y_a)] y_ = [y_a] pre_y = y_a for i in range(3): x = a + i * h k1 = h * function(x, pre_y) k2 = h * function(x + h / 2, pre_y + k1 / 2) k3 = h * function(x + h / 2, pre_y + k2 / 2) k4 = h * function(x + h, pre_y + k3) y = (k1 + 2 * k2 + 2 * k3 + k4) / 6 + pre_y y_dot.append(function(x + h, y)) y_.append(y) pre_y = y return y_, y_dot def solveByMilne(f, epsilon, a, y_a, b): function = Result.get_function(f) n = 2 delta = float("inf") while abs(delta) >= epsilon / 10: n *= 2 h = (b - a) / n y, y_dot = Result.runge_kutta(h, a, y_a, function) for i in range(n - 3): y.append(y[-4] + 4 * h / 3 * (2 * y_dot[-1] - y_dot[-2] + 2 * y_dot[-3])) y_dot.append(function(a + (i + 4) * h, y[-1])) y[-1] = y[-3] + h / 3 * (y_dot[-1] + 4 * y_dot[-2] + y_dot[-3]) delta = y[-1] - y[-2] y_dot[-1] = function(a + (i + 4) * h, y[-1]) return y[-1] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
class ODE: def __init__(self, function, x0, y0): self.function = function self.x0 = x0 self.y0 = y0 def euler(self, h, step): result = [] pre_y = self.y0 for i in range(step): x = self.x0 + i * h fy = self.function(x, pre_y) y = pre_y + h * fy result.append(y) pre_y = y # print("y_{} = {}".format(i + 1, y)) return result def eulerImproved(self, h, step): result = [] pre_y = self.y0 pre_fy = self.y0 for i in range(step): x = self.x0 + (i + 1 / 2) * h yHalf = pre_y + pre_fy * h / 2 fyHalf = self.function(x, yHalf) y = pre_y + h * fyHalf result.append(y) pre_y = y pre_fy = function(x + h / 2, y) # print("y_{} = {}".format(i + 1, y)) return result def runge_kutta(self, h, step): result = [] pre_y = self.y0 for i in range(step): x = self.x0 + i * h k1 = h * self.function(x, pre_y) k2 = h * self.function(x + h / 2, pre_y + k1 / 2) k3 = h * self.function(x + h / 2, pre_y + k2 / 2) k4 = h * self.function(x + h, pre_y + k3) y = (k1 + 2 * k2 + 2 * k3 + k4) / 6 + pre_y result.append(y) pre_y = y # print("y_{} = {}".format(i + 1, y)) return result def milne(self, h, step): y = [self.y0] + self.runge_kutta(h, 3) y_dot = [function(self.x0 + i * h, y[i]) for i in range(4)] for i in range(step - 3): y.append(y[-4] + 4 * h / 3 * (2 * y_dot[-1] - y_dot[-2] + 2 * y_dot[-3])) y_dot.append(function(self.x0 + (i + 4) * h, y[-1])) y[-1] = y[-3] + h / 3 * (y_dot[-1] + 4 * y_dot[-2] + y_dot[-3]) y_dot[-1] = function(self.x0 + (i + 4) * h, y[-1]) # print("y_{} = {}".format(i + 4, y[-1])) return y[1:] def adams(self, h, step): y = [self.y0] + self.runge_kutta(h, 3) y_dot = [function(self.x0 + i * h, y[i]) for i in range(4)] for i in range(step - 3): y.append(y[-1] + (55 * y_dot[-1] - 59 * y_dot[-2] + 37 * y_dot[-3] - 9 * y_dot[-4]) / 24 * h) y_dot.append(function(self.x0 + (i + 4) * h, y[-1])) # print("y_{} = {}".format(i + 4, y[-1])) return y[1:] if __name__ == "__main__": function = lambda x, y: y - 2 * x / y x0 = 0 y0 = 1 h = 0.002 step = 1000 ode = ODE(function, x0, y0) print(ode.euler(h, step)[-1]) print("--------------------------------------------") print(ode.eulerImproved(h, step)[-1]) print("--------------------------------------------") print(ode.runge_kutta(h, step)[-1]) print("--------------------------------------------") print(ode.adams(h, step)[-1]) print("--------------------------------------------") print(ode.milne(h, step)[-1]) |
题目:一共N个货币种类,当前为S种类的货币,当前拥有的该货币价值为V,下面M行包含可以交换货币的汇率/手续费信息:A,B,RAB,CAB,RBA,CBA分别代表种类A,种类B,A到B的汇率,A到B的手续费,B到A的汇率,B到A的手续费。求能否通过某种交换方式能让货币越来越多即大于V。
解析:Bellman-Ford。修改#1450代码即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include<iostream> #include<vector> #include<queue> #define INF 0x3f3f3f3f using namespace std; int n, m, s; float v; struct Currency { Currency() { money = INF; done = false; } //neighbor_index rate commission vector<pair<int, pair<float, float>>> neighbor; float money; bool done; }currency[100]; void SPFA() { queue<int> q; currency[s - 1].money = -v; q.push(s - 1); while (!q.empty()) { int x = q.front(); q.pop(); currency[x].done = false; for (auto ptr = currency[x].neighbor.begin(); ptr != currency[x].neighbor.end(); ptr++) { if (currency[ptr->first].money > -(-currency[x].money - ptr->second.second) * ptr->second.first) { currency[ptr->first].money = -(-currency[x].money - ptr->second.second) * ptr->second.first; if (!currency[ptr->first].done) { currency[ptr->first].done = true; q.push(ptr->first); } } } } if (currency[s - 1].money < v)return; } int main() { //货币种类 下面共几行 当前货币种类 当前拥有的该货币价值 cin >> n >> m >> s >> v; for (int i = 0; i < m; i++) { int a, b; float rab, cab, rba, cba; cin >> a >> b >> rab >> cab >> rba >> cba; currency[a - 1].neighbor.push_back(make_pair(b - 1, make_pair(rab, cab))); currency[b - 1].neighbor.push_back(make_pair(a - 1, make_pair(rba, cba))); } SPFA(); if (-currency[s - 1].money <= v) cout << "NO" << endl; else cout << "YES" << endl; } |
浙ICP备2021019730-1 浙公网安备 33010902002953号
Copyright © 2022 PanCake