BOJ 2775 부녀회장이 될테야

1 개요[ | ]

BOJ 2775 부녀회장이 될테야
  • 층과 거주자 수의 규칙을 찾는 문제

2 Java[ | ]

import java.util.Scanner;
public class Main {
    private static int getPopulation(int k, int n) {
        if( k == 0 ) return n;
        int population = 0;
        for(int i=1; i<=n; i++) {
            population += getPopulation(k-1, i);
        }
        return population;
    }
    
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int T = sc.nextInt();
		for (int i=0; i<T; i++) {
		    int k = sc.nextInt();
		    int n = sc.nextInt();
	        System.out.println( getPopulation(k,n) );
		}
	}
}

3 Perl[ | ]

sub getPopulation {
    my ($k, $n) = @_;
    return $n if ($k eq 0);
    my $population = 0;
    $population += getPopulation($k-1, $_) for (1..$n);
    return $population;
}

$T=<>;
for (1..$T) {
    my $k=<>;
    my $n=<>;
    printf("%d\n", getPopulation($k, $n));
}

4 PHP[ | ]

<?php
for($i=1; $i<15; $i++) {
    $arr[0][$i] = $i;
    $arr[$i][1] = 1;
}
for($i=1; $i<15; $i++) {
    for($j=2; $j<15; $j++) {
         $arr[$i][$j] = $arr[$i][$j-1] + $arr[$i-1][$j];
    }
}
$TT = intval(fgets(STDIN));
for($t=0; $t<$TT; $t++) {
    $k = intval(fgets(STDIN));
    $n = intval(fgets(STDIN));
    echo $arr[$k][$n] . "\n";
}
<?php
function factorial($x) {
	$result = 1;
	for($i=2; $i<=$x; $i++) $result *= $i;
	return $result;
}
$TT = intval(fgets(STDIN));
for($t=0; $t<$TT; $t++) {
    $k = intval(fgets(STDIN));
    $n = intval(fgets(STDIN));
    echo factorial($k+$n)/factorial($k+1)/factorial($n-1) . "\n";
}

5 Python[ | ]

from math import factorial
TT = int(input())
for t in range(TT):
    k = int(input())
    n = int(input())
    print( factorial(k+n) // factorial(k+1) // factorial(n-1) )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}