Computer/Coding Test
Excercism - Interest is Interesting 정수형(float, double, decimal)
안녕도라
2025. 1. 14. 16:15
문제요약
통장 이율에 따라 목표 금액에 도달하기 까지 걸리는 년수 구하기
난이도
?
풀이 포인트
- 실수형 변환 : 더 큰 것으로 변환하여 계산 (float을 decimal로 바꾸어 계산)

REVIEW
아무래도 영어라, 문제가 한 눈에 읽히지 않았다.
마지막 함수를 짜는 데 좀 해맸다.
target에 도달하기 까지 반복을 돌리며 year을 증가시키는 방법도 간단한 건데!
CODE
using System;
static class SavingsAccount
{
// 잔고에 따른 이율 반환
public static float InterestRate(decimal balance)
{
if (balance < 0){
return 3.213f;
}else if (balance < 1000){
return 0.5f;
}else if (balance < 5000){
return 1.621f;
}else {
return 2.475f;
}
}
// 이율에 따른 이자 계산
public static decimal Interest(decimal balance)
{
return balance * (decimal)InterestRate(balance) / 100;
}
// 원금 + 이자
public static decimal AnnualBalanceUpdate(decimal balance)
{
return balance + Interest(balance);
}
//target 금액까지 걸리는 년수 계산
public static int YearsBeforeDesiredBalance(decimal balance, decimal targetBalance)
{
int years = 0;
// target금액에 도달할 때까지 잔고를 갱신해 가며 비교
while (balance < targetBalance)
{
balance = AnnualBalanceUpdate(balance);
years++;
}
return years;
}
}