Medium

You have deposited a specific amount of money into your bank account. Each year your balance increases at the same growth rate. With the assumption that you don't make any additional deposits, find out how long it would take for your balance to pass a specific threshold.

Example

For deposit = 100, rate = 20, and threshold = 170, the output should be
depositProfit(deposit, rate, threshold) = 3.

Each year the amount of money in your account increases by 20%. So throughout the years, your balance would be:

  • year 0: 100;
  • year 1: 120;
  • year 2: 144;
  • year 3: 172.8.

Thus, it will take 3 years for your balance to pass the threshold, so the answer is 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer deposit

    The initial deposit, guaranteed to be a positive integer.

    Guaranteed constraints:
    1 ≤ deposit ≤ 100.

  • [input] integer rate

    The rate of increase. Each year the balance increases by the rate percent of the current sum.

    Guaranteed constraints:
    1 ≤ rate ≤ 100.

  • [input] integer threshold

    The target balance.

    Guaranteed constraints:
    deposit < threshold ≤ 200.

  • [output] integer

    • The number of years it would take to hit the threshold.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

임의로 주어진 매개변수형들을 int형에서 double형으로 변환하여 풀었다.

int depositProfit(double deposit,double rate,double threshold)
{
	int i;

	for(i=0;deposit<threshold;i++)
	{
		deposit*=(rate+100.0);
		deposit/=100.0;
	}

	return i;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> stringsRearrangement  (0) 2020.04.05
<Codesignal> absoluteValuesSumMinimization  (0) 2020.04.05
<Codesignal> Circle of Numbers  (0) 2020.04.05
<Codesignal> chessBoardCellColor  (0) 2020.04.05
<Codesignal> alphabeticShift  (0) 2020.04.05

+ Recent posts