【a702】贷款利率

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 second

typedef 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;
            }
        else
            r = m;
    }
    ans*=100;
    printf("%.1lf
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626861.html