HR30 Day 9: Recursion

1 개요[ | ]

Day 9: Recursion
해커랭크 30 Days of Code
문제 풀이
0-9 Day e
HR30 Day 0: Hello, World.

HR30 Day 1: Data Types

HR30 Day 2: Operators

HR30 Day 3: Intro to Conditional Statements

HR30 Day 4: Class vs. Instance

HR30 Day 5: Loops

HR30 Day 6: Let's Review

HR30 Day 7: Arrays

HR30 Day 8: Dictionaries and Maps

HR30 Day 9: Recursion

2 Java[ | ]

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {
    // Complete the factorial function below.
    static int factorial(int n) {
        if( n <= 1 ) return 1;
        return n * factorial(n-1);
    }
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        int result = factorial(n);
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
        bufferedWriter.close();
        scanner.close();
    }
}

3 PHP[ | ]

<?php
function factorial($n) {
    if( $n <= 1 ) return 1;
    return $n * factorial($n-1);
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $n);
$result = factorial($n);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);

4 Python[ | ]

#!/bin/python3
import math
import os
import random
import re
import sys

# Complete the factorial function below.
def factorial(n):
    if n<=1: return 1
    return n * factorial(n-1)
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    n = int(input())
    result = factorial(n)
    fptr.write(str(result) + '\n')
    fptr.close()
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}