카타 7급 Growth of a Population

1 C[ | ]

int nbYear(int p0, double percent, int aug, int p) {
  int year = 0;
  while( p0 < p ) {
    p0 += floor(p0 * percent / 100) + aug;
    year++;
  }
  return year;
}
int nbYear(int p0, double percent, int aug, int p) {
  if(p0 >= p) return 0;
  return nbYear((p0 + p0 * percent/100 + aug), percent, aug, p) + 1;
}

2 Kotlin[ | ]

package growth
fun nbYear(pp0:Int, percent:Double, aug:Int, p:Int):Int {
  var year = 0
  var p0 = pp0
  while( p0 < p ) {
    p0 = p0 + (p0 * percent / 100).toInt() + aug
    year++
  }
  return(year)
}
package growth
fun nbYear(pp0:Int, percent:Double, aug:Int, p:Int):Int = generateSequence(pp0) { it + (applyPercent(percent, it)) + aug }.indexOfFirst { it >= p }
val applyPercent: (Double, Int) -> Int = { a, b  -> (a/100*b).toInt()}
package growth
fun nbYear(pp0: Int, percent: Double, aug: Int, p: Int): Int = 
  generateSequence(pp0.toDouble()) { it * (1 + percent / 100) + aug }.takeWhile { it < p }.count()
package growth
fun nbYear(p0: Int, percent: Double, aug: Int, p: Int): Int {
    tailrec fun iter(nYears: Int, curP: Int): Int =
        if (curP >= p) nYears
        else iter(nYears + 1, (curP * (100 + percent) / 100).toInt() + aug)
    return iter(0, p0)
}

3 R[ | ]

nbYear <- function (p0, percent, aug, p) {
  year = 0
  while( p0 < p ) {
    p0 = p0 + floor(p0 * percent / 100) + aug
    year = year + 1
  }
  return(year)
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}