自建站怎么搭建廈門網(wǎng)站流量優(yōu)化價格
原題鏈接:https://www.luogu.com.cn/problem/P1163
目錄
1. 題目描述
2. 思路分析
3. 代碼實現(xiàn)
1. 題目描述
2. 思路分析
這題需要注意的是利率按月累計這句話,也就是相當(dāng)于“利滾利”。
我們定義sum變量表示貸款原值,money表示每月支付的分期付款金額,month表示還清貸款需要的月數(shù)。
寫一個自定義函數(shù)check()用來判斷遍歷時的利率和題目實際利率的大小關(guān)系,將利率區(qū)間設(shè)為[0,10],即l=0,r=10(r也可以更大)。然后進行浮點數(shù)二分,
如果check()函數(shù)返回值大于0,說明利率過大,則從左側(cè)繼續(xù)二分查找(r=mid);
如果check()函數(shù)返回值小于0,說明利率過小,則從右側(cè)繼續(xù)二分查找(l=mid);
如果check()函數(shù)返回值等于零,則輸出結(jié)束程序。
浮點數(shù)二分模板在這https://blog.csdn.net/m0_62531913/article/details/132391682?spm=1001.2014.3001.5501
又因為答案要四舍五入精確到0.1%,所以我們最后輸出l*100即可。
3. 代碼實現(xiàn)
#include<bits/stdc++.h>
using namespace std;
int sum, money, month;
bool check(double x)
{double s = sum;for (int i = 0; i < month; i++){s = s * (1 + x) - money;}if (s > 0) return true;else return false;
}int main()
{cin >> sum >> money >> month;double l = 0, r = 10;while (r - l > 1e-4){double mid = (l + r) / 2;if (check(mid)) r = mid;else l = mid;}printf("%.1lf\n", l * 100);return 0;
}