본문 바로가기

프로그래밍/자바

[자바] 정올 587 ~ 592 : 함수3 - 자가진단1~6

728x90
반응형
SMALL

자가진단1 : 587

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int i = 0;
        solution(n, i);
    }
    public static void solution(int n, int i){
        if(i == n) return;

        System.out.println("recursive");
        i++;
        solution(n, i);
    }
}

자가진단2: 588

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int i = n;
        solution(i);
    }
    public static void solution(int i){
        if(i == 0) return;

        System.out.print(i + " ");
        i--;
        solution(i);
    }
}

자가진단3: 589

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int answer = solution(n);
        System.out.println(answer);
    }
    public static int solution(int n){
        if(n==1) return 1;
        else if(n>1) return solution(n-1) + solution(n-2);
        else return 0;
    }
}

자가진단4: 592

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int answer = solution(n);
        System.out.println(answer);
    }
    public static int solution(int n){
        if(n > 0) return (int)Math.pow(n%10,2) + solution(n/10);
        else return 0;
    }
}

자가진단5 : 590

import java.util.Scanner;
public class Main {
    public static int N;
    public static int[] arr = new int[11];

    static void dice(int n){

        if(n > N) {
            for(int i=1; i<=N; i++) System.out.print(arr[i] + " ");
            System.out.println();
            return ;
        }
        for(int i = arr[n-1]; i<=6; i++){
            arr[n] = i;
            dice(n+1);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        arr[0] =1;
        dice(1);
    }
}

자가진단6 : 591

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(solution(n));
    }
    public static int solution(int n){
        if(n == 1) return 1;
        else return solution(n/2) + solution(n-1);
    }
}
728x90
반응형
LIST