列表

详情


NC54132. 人生

描述

刘会长相亲结束了,却因被对方嫌弃没有房子而惨败归来,刘会长不甘这份屈辱,决定好好工作早日买房,已知刘会长的月工资为p,房价为m,由于不可控因素,房价每月上涨d%(从第二个月开始),安排下次相亲时间为n月之后,求问刘会长下次相亲之前能不能买房,我们假定刘会长初始余额为0(事实也是如此)。

输入描述

输入有多组,每组一行,四个数p,m,d,n。

输出描述

每组输出一行,如果刘会长下次相亲之前可以买房,则输出一个整数,表示刘会长最早在第几个月可以买房,否则刘会长就会感叹道“Maybe this is the true life.”。

示例1

输入:

15 20 50 5

输出:

2

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 468K, 提交时间: 2019-10-20 16:13:52

#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<string>

using namespace std;
int  main(){
double p,n,m,d;
while (cin>>p>>m>>d>>n)
{
	int l=0;
	for(int i=1;i<=n;++i){
		double t=i*p;
        if(i!=1)
        m=m*(100+d)/100;
      if(t>=m) {  l=1;printf("%d\n",i); break; }
            }
if(!l) printf("Maybe this is the true life.\n");
}
return 0;
}

Python3(3.5.2) 解法, 执行用时: 24ms, 内存消耗: 3428K, 提交时间: 2019-10-21 09:08:10

while 1:
    try:
        a, b, c, d = map(float, input().split())
        e, f = 0., 1
        for i in range(1, int(d) + 1):
            e += a
            if e >= b:
                print(i)
                f = 0
                break
            b *= (1 + c / 100)
        if f:
            print('Maybe this is the true life.')
    except:
        break

C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 348K, 提交时间: 2020-01-18 02:23:30

#include<iostream>
using namespace std;
int main()
{double p,m,d,n,moneny=0;
while(cin>>p>>m>>d>>n)
{for(int i=1;i<=n;i++)
{moneny=p*i;
 if(moneny>=m) {cout<<i<<endl;break;}
 m*=(1+0.01*d);}
 if(moneny<m) cout<<"Maybe this is the true life."<<endl;}}

上一题