CodeStudy 📖

[프로그래머스] Lv.0 PCCE 기출문제 (1-4번)

봄설날 2024. 8. 20. 14:20

파이썬 밖에 할 줄 몰라서 Java로 새로 코딩테스트 연습을 해보려고 한다.

학교에서 배웠던 기억을 되살려보고자 아주 기초부터!! 그래서 Lv.0 부터 하나씩 시작해본다.

 

#1 출력

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        String msg = "Spring is beginning";
        int val1 = 3;
        String val2 = "3";
        
        System.out.println(msg);
        System.out.println(val1 + 10);
        System.out.println(val2 + "10");
    }
}

** 문자를 출력할 때에는 "" 꼭 붙이기!

 

 

#2 피타고라스의 정리

 

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int c = sc.nextInt();

        int b_square = c*c - a*a;
		//int b_square = (int)Math.pow(c, 2) - (int)Math.pow(a, 2);
        System.out.println(b_square);
    }
}

 

** 제곱을 표현 할 때는 Math.pow 를 사용하자!!

몰라서 c*c를 사용했다. c^2도 해봤는데 얜 안된다!!

 

#3 나이 계산

 

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();
        String age_type = sc.next();
        int answer = 0;

       if (age_type == "Korea") {
        answer = 2030 - year + 1	;
		}
        }
        else if (age_type.equals("Year")) {
            
answer = 2030 - year
;
        }

        System.out.println(answer);
    }
}

** 쉬운 문제!!

 

#4 저축

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int start = sc.nextInt();
        int before = sc.nextInt();
        int after = sc.nextInt();

        int money = start;
        int month = 1;
        while (money < 70) {
            money += 
before
;
            month++;
        }
        while (
money
 < 100) {
            
money += after
;
            month++;
        }

        System.out.println(month);
    }
}

** 얘도 쉬운거