[Java] SWEA 1217 거듭 제곱

Lee GaEun·2024년 11월 13일

[Java] 알고리즘

목록 보기
16/93

1217 거듭 제곱 문제 링크

문제분석

  • 두 개의 숫자 N, M이 주어질 때, N의 M 거듭제곱 값을 구하는 프로그램을 재귀호출을 이용하여 구현
  • 예시
    • 2 5 = 2 X 2 X 2 X 2 X 2 = 32
    • 3 6 = 3 X 3 X 3 X 3 X 3 X 3 = 729

제약 사항

  • java: import java.util.Scanner;
  • 를 제외한 나머지 라이브러리 사용을 금지
  • 테스트 케이스는 10개
  • 결과 값은 Integer 범위를 넘지 않음

입력 조건

첫째 줄 : 테스트 케이스 번호
둘째 줄 : 숫자 N, M 두 개

출력 조건

#부호 + 테스트 케이스 번호 + " " + 거듭제곱 결과 출력


#1

import java.util.Scanner;
 
class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        /*int T;
        T=sc.nextInt();*/
 
        for(int test_case = 1; test_case <= 10; test_case++)
        {
            int N = sc.nextInt();
            int num = sc.nextInt();
            int x = sc.nextInt();
 
            for(int i=1; i<x; i++) {
                num=num*num;
            }
 
            System.out.println("#" + N + " " + num);
        }
    }
}
  • 실패

#2

  • 아니 num 값을 훼손해놓고 num에다 또 num을 곱하고 있네...

import java.util.Scanner;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        /*int T;
        T=sc.nextInt();*/

        for(int test_case = 1; test_case <= 10; test_case++)
        {
            int N = sc.nextInt();
            int num = sc.nextInt();
            int result = 1;
            int x = sc.nextInt();

            for(int i=0; i<x; i++) {
                result *= num;
            }

            System.out.println("#" + N + " " + result);
        }
    }
}

  • 성공(6M)

#3

  • 성공했는데,,, 문제분석 똑바로 안 하냐,,
  • 재귀로 풀라고 써있잖아...

import java.util.Scanner;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        /*int T;
        T=sc.nextInt();*/

        for(int test_case = 1; test_case <= 10; test_case++)
        {
            int N = sc.nextInt();
            int num = sc.nextInt();
            int result;
            int x = sc.nextInt();

            result = calculate(num, x, 0, 1);

            System.out.println("#" + N + " " + result);
        }
    }
    static int calculate(int num, int x, int i, int result) {
        if(x==i) return result;
        return calculate(num, x, i+1, result * num);
    }
}

  • 재귀로 다시 함..

#4

3분


import java.util.HashMap;
import java.util.Scanner;
import java.io.FileInputStream;
import java.util.Set;

class Solution
{
    static char[][] arr;
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=10;

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            int x = sc.nextInt();
            int y = sc.nextInt();
            int answer = 1;
            for(int i=0; i<y; i++) {
                answer *= x;
            }

            System.out.println("#"+test_case+" "+answer);
        }
    }
}

  • 와,, 저때 반성 해놓고 귀신같이 또 재귀없이 그냥 풀었네
  • 문제가 너무 간단한 탓인가,,
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글