NC54132. 人生
描述
输入描述
输入有多组,每组一行,四个数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;}}