题意:有n个灯笼,第一个的高度是A,最后一个是B,灯笼的关系给出,并要求每个灯笼的高度是非负数的,求最低的B
思路:推出公式:H[i]=2*H[i-1]+2-H[i-2],然后枚举H[2],在知道H[1]的情况下就能求出所有的高度,然后判断是否都是非负数
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int MAXN = 1500; int n; double A, B, H[MAXN]; int check(double cnt) { H[1] = cnt; for (int i = 2; i < n; i++) { H[i] = 2*H[i-1] + 2 - H[i-2]; if (H[i] < 0) return 0; } B = H[n-1]; return true; } int main() { while (scanf("%d%lf", &n, &A) != EOF) { H[0] = A; double l = -1, r = MAXN; while (r-l > 1e-6) { double mid = (l+r)/2; if (check(mid)) r = mid; else l = mid; } printf("%.2lf\n", B); } return 0; }
uva 1555 Garland,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u011345136/article/details/37073939