Week11 Homework—A-Required Questions 11-1—

topic

Mr. Suantou starts working now, with an annual salary of NNN million. He hopes to buy a 606,060 square meter house near the garlic factory. The current price is 200,02 million. Assuming that the house price increases by KKK per year per year, and Mr. Garlic’s future annual salary remains the same, he does not eat or drink, does not have to pay taxes, and that all his NNN income is accumulated every year. How many years can I buy this house? (In the first year, the annual salary is NNN million, and the house price is 200,202 million)

Input format

One line, containing two positive integers N(10≤N≤50)N(10 \le N \le 50)N(10≤N≤50), K(1≤K≤20)K(1 \le K \le 20) K (1≤K≤20), separated by a single space.

Output format

If the house can be bought in 202020 or before, an integer MMM is output, indicating that it needs to be bought in the MMM year at the earliest; otherwise, "Impossible" is output. The extra spaces at the end of each line during output will not affect the correctness of the answer

Sample input

50 10

Sample output

8

Ideas

The for loop continuously calculates the deposit and house price of the i-th year, jumps out of the loop when it can be bought, and sets the flag to 1, otherwise no processing is performed. Finally, according to the flag value, it outputs whether it can be bought and the year it can be bought.

Code

#include<iostream>
using namespace std;
float price=200;
int main() 
{
    
    
 int n,k,sum=0;;
 cin>>n>>k;
 int flag=0;
 int i;
 for(i=1;i<=20;i++)
 {
    
    
  sum+=n;
  if(sum>=price)
  {
    
    
   flag=1;
   break;
  }
  price+=price*k/100.0;
 }
 if(flag==0)
  cout<<"Impossible"<<endl;
 else
  cout<<i<<endl;
 return 0;
}

Guess you like

Origin blog.csdn.net/alicemh/article/details/105889826