标签:str printf long ace col for 代码 math color
题意:N个灯泡离地H_i,满足H1 = A ,Hi = (Hi-1 + Hi+1)/2 – 1,HN = B ,求最小B。
思路:
解决问题的代码:
#include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> #include <cstring> using namespace std; #define maxa 1000 #define maxn 1000+16 typedef long long ll; int n; double a, b, h[maxn]; bool c(double mid) { h[1] = mid; for (int i = 2; i < n; i++) { h[i] = 2 * h[i - 1] + 2 - h[i - 2]; if (h[i] < 0) return false; } b = h[n - 1]; return true; } int main() { cin >> n >> a; h[0] = a; double lb = -1, ub = maxa + 16; for (int i = 0; i < 100; i++) { double mid = (lb + ub) / 2; if (c(mid)) ub = mid; else lb = mid; } printf("%.2lf\n", b); return 0; }
标签:str printf long ace col for 代码 math color
原文地址:https://www.cnblogs.com/xuxiaojin/p/9425942.html