1.5 16: Buy a house

Describe
a programmer who started working with an annual salary of N million. He hopes to buy a 60-square-meter house in the Zhongguancun Mansion. The current price is 2 million. Assuming that the house price increases by K% per year, and the programmer’s future annual salary remains unchanged, I don’t eat or drink, I don’t have to pay taxes. I save up all my income every year. How many years can I buy this house? (In the first year, the annual salary is N million, and the house price is 2 million)

Enter a
line containing two positive integers N (10 <= N <= 50), K (1 <= K <= 20), separated by a single space.
Output
If the house can be bought in the 20th year or before, an integer M is output, indicating that it needs to be bought in the M year at the earliest, otherwise Impossible is output.
Sample input
50 10
Sample output
8

#include <iostream>
using namespace std;
int main()
{
    
    
	int n,k,y=20,i;
	double fangjia=200;
	cin >>n>>k;
	
	for(i=1;i<=y;i++)
	{
    
    	
		if(i*n>=fangjia)
		{
    
    
			cout<<i<<endl;
			break;
		}			
		fangjia *= (1+1.0*k/100);//注意这里不要忘记家前面的1.0,也可以写成0.01*k
	}
	if(i==21)
		cout<<"Impossible"<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/yansuifeng1126/article/details/112011875