C++ 实现浴缸水温控制算法
#include
int t, t2, x, z, to;
double solve(double y1, double y2) //求解函数 { double t1 = t + (t2 - t) * (y1 / (y1 + y2)) * (y1 / x) / ((y1 / x) + (y2 / (2 * x))); double t2 = t + (t2 - t) * (y2 / (y1 + y2)) * (y2 / (2 * x)) / ((y1 / x) + (y2 / (2 * x))); return (t1 + t2) / 2.0; }
int main() { cin >> t >> t2 >> x >> z >> to; double maxt = t2 + (to - t2) / 2.0; //最大可能的水温 double ans = -1, ans1 = -1, ans2 = -1; //ans表示最大的总流速 for (double y1 = 0; y1 <= z; y1++) //枚举y1和y2 { for (double y2 = 0; y2 <= z; y2++) { if (y1 + y2 > z) break; //总流速超过限制 double temp = solve(y1, y2); if (temp >= to && temp <= maxt) //符合条件 { double temp2 = y1 + y2; //更新答案 if (temp2 > ans) { ans = temp2; ans1 = y1; ans2 = y2; } } } } printf("%.0lf %.0lf\n", ans1, ans2); //保留整数输出 return 0; }
原文地址: https://www.cveoy.top/t/topic/lPnG 著作权归作者所有。请勿转载和采集!