Time Limit: 10 second
Memory Limit: 2 MB
問題描述
當一個人從銀行貸款后,在一段時間內他將不得不每月嘗還固定的分期付款。這個問題要求計算機出貸款者向銀行支付的利率。假設利率按月累計。
Input
輸入文件 僅一行包含三個用空格隔開的正整數。 第一個整數表示貸款的原值a,第二個整數表示每月支付的分期付款金額b,第三個整數表示分期付款還清貸款所需的總月數m。(1
Output
輸出文件應該是一個實數,表示該貸款的月利率(用百分數表示),四舍五入精確到0.1%
Sample Input
1000 100 12
Sample Output
2.9
【題目鏈接】:http://noi.qz5z.com/viewtask.asp?id=a702
【題解】
每個月需要在b當中扣除還沒還完的錢(a)*月利率(x);
然后再用被扣掉一部分的b去減a;
重復上述過程;
知道怎么算之后就二分利率是多少;
然后看看利率為x的時候要還多少天f(x);
如果f(x)<=c則可以讓利率再高一點以讓還多少天接近c;
【完整代碼】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se secondtypedef pair<int,int> pii;
typedef pair<LL,LL> pll;void rel(LL &r)
{r = 0;char t = getchar();while (!isdigit(t) && t!='-') t = getchar();LL sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}void rei(int &r)
{r = 0;char t = getchar();while (!isdigit(t)&&t!='-') t = getchar();int sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);int a,b,c;int f(double x)
{double ta = a;int month= 0;while (ta>0){double temp = b;temp-=ta*x;if (temp<0)return 21e8;ta-=temp;month++;}return month;
}int main()
{//freopen("F:\\rush.txt","r",stdin);scanf("%d%d%d",&a,&b,&c);double l = 0.0,r = 1.0;double ans = 0.0;while (r-l>=0.0001){double m = (l+r)/2.0;if (f(m)<=c){ans = m;l = m;}elser = m;}ans*=100;printf("%.1lf\n",ans);return 0;
}