题意略。
思路:二分。注意当利率高且m比较小的时候,每个月的偿还可能会大于本金,所以我们二分的右边界应该要设为2 * 本金。
详见代码:
#include#define eps 1e-7using namespace std;double s,p;int m;int sgn(double x){ if(fabs(x) < 0) return 0; if(x > 0) return 1; else if(x < 0) return -1;}bool jud(double x){ double fir = x * m; double ai = s * p,bi = x - ai; double sum1 = ai,sum2 = bi; for(int i = 2;i <= m;++i){ ai = (s - sum2) * p; bi = (x - ai); sum1 += ai; sum2 += bi; } return sgn(s - sum2) <= 0;}int main(){// freopen("input.txt","r",stdin);// freopen("output.txt","w",stdout); scanf("%lf%d%lf",&s,&m,&p); double l = 0,r = 2 * s,mid; p /= 100.0; while(fabs(r - l) > eps){ mid = (r + l) / 2; if(jud(mid)) r = mid; else l = mid; } printf("%lf\n",l); return 0;}/*100 1 50*/