银行贷款
2024年12月25日小于 1 分钟
枚举每个利率
#include <bits/stdc++.h>
using namespace std;
double w0, w, m;
bool check(double p)
{
double ww0 = w0;
for (int i = 1; i <= m; i++)
ww0 = (ww0 * (1 + p) - w);
return ww0 <= 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> w0 >> w >> m;
double ans;
for (double p = 0.0001;; p += 0.0001)
{
if (check(p))
ans = p;
else
break;
}
cout << fixed << setprecision(1) << ans * 100;
return 0;
}
二分
#include <bits/stdc++.h>
using namespace std;
double w0, w, m;
bool check(double p)
{
double ww0 = w0;
for (int i = 1; i <= m; i++)
ww0 = (ww0 * (1 + p) - w);
return ww0 <= 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> w0 >> w >> m;
double l = 0;
double r = 3;
while (r - l > 0.0001)
{
double mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}
cout << fixed << setprecision(1) << l * 100;
return 0;
}