본문 바로가기

코딩/JAVA Baek joon Code

JAVA - Baek joon Code [1]

백준 단계별로 풀어보기 1단계입니다.

 

총 11문제로 이루어져 있으며, 거의 쉬운 난이도로 이루어진듯 하지만 예상 외로 1-1번 문제의 정답률이 낮습니다.

 

그 이유는 백준에서는 public class 를 Main으로 설정해야 컴파일 에러가 뜨지 않는다는 사실을 모르기 때문입니다.

 

더보기

이클립스에서는 파일명과 public class 이름이 같게 설정되어 나오며, 그 이름은 사용자가 자유롭게 설정할 수 있다. 그러나 백준에서는 개인이 코드를 적으면서 개발하는 것이 아니고, 조건에 맞게 문제를 맞추었는가 확인을 해야한다. 그래서 같은 기준을 적용해야 할 필요가 있기 때문에 Main으로 통일을 하게끔 한 것이 아닌가 생각해 본다.

 

내가 쓴 것 맞지는 않으니 그냥 이 사람은 이렇게 풀었구나~ 정도로 봐주면 좋겠습니다.

 

1-1

class Hello{
    public void Print(){
        System.out.printf("Hello World!\n");
    }
}

public class Main{
    public static void main(String[] args) {
        Hello hi = new Hello();
    
        hi.Print();
    }
}

그냥 Main에 다 넣어서 해도 되지만 위에 쓴 것처럼 필자도 Main으로 설정을 하지 않아서 컴파일 에러의 향연 속에서 헤어 나오지 못하고 있었습니다. 그래서 따로 클래스를 만들어야 하는건가? 라는 생각을 해서 좀 불편한 방식으로 하게 됐습니다.

 

1-2

class Military{
    
    public void Prin(){
        System.out.printf("강한친구 대한육군\n");
    }
    
}

public class Main{
    public static void main(String[] args){
        Military army = new Military();
        
        army.Prin();
        army.Prin();
    }
}

문구를 보고 흠칫했다.

2번 문제부터는 정답률이 올라갔습니다. 1번에서 Main에 대한 사실을 깨닫게 된 것으로 보입니다.

 

1-3

public class Main{
    public static void main(String[] args){
        System.out.println("\\    /\\");
        System.out.println(" )  ( \')");
        System.out.println("(  /  )");
        System.out.println(" \\(__)|");
    }
}

이 문제는 아무 생각없이 하다가는 오답세례를 받을 것입니다.

특수한 문자들을 print 하기 위해서는 입력하고자 하는 문자 앞에 \를 붙여 주어야 합니다.

 

예를 들어서 "(큰 따옴표)를 출력하고 싶으면 "\"" 이렇게 해서 출력할 수 있습니다.

더보기

$  ^  *  (  )  +  |  [  {  등이 이에 해당된다.

 

1-4

public class Main{
    public static void main(String[] args){
        System.out.println("|\\_/|");
        System.out.println("|q p|   /}");
        System.out.println("( 0 )\"\"\"\\");
        System.out.println("|\"^\"`    |");
        System.out.println("||_/=\\\\__|");
    }
}

1-3과 유사합니다.

 

 

1-5

import java.util.Scanner;

class Sum{
    private int a = 0;
    private int b = 0;
    
    public void Input(int x,int y){
        a = x;
        b = y;
    }
    public int Suming(){
        return a + b;
    }
        
}

public class Main{
    public static void main(String[] args){
        Sum su = new Sum();
        
        Scanner sc = new Scanner(System.in);
        
        int x,y,result;
        
        x = sc.nextInt();
        y = sc.nextInt();
        
        su.Input(x,y);
        result = su.Suming();
        
        System.out.printf("%d\n",result);
    }
}

 

간단해 보이는 요구사항입니다. 두 개의 수를 입력받아 합을 출력해 주는 코드입니다.

 

나는 따로 Sum class를 만들어서 Input 메서드로 숫자들을 입력받고 그것을 더해서 리턴해 주는 방식을 사용하였습니다.

 

 

1-6

import java.util.Scanner;

class Subtract{
    private int a = 0;
    private int b = 0;
    
    public void Input(int x,int y){
        a = x;
        b = y;
    }
    public int Subtracting(){
        return a - b;
    }
        
}

public class Main{
    public static void main(String[] args){
        Subtract su = new Subtract();
        
        Scanner sc = new Scanner(System.in);
        
        int x,y,result;
        
        x = sc.nextInt();
        y = sc.nextInt();
        
        su.Input(x,y);
        result = su.Subtracting();
        
        System.out.printf("%d\n",result);
    }
}

1-5와 비슷한 방식입니다. 단지 연산만 다를 뿐..

 

 

1-7

import java.util.Scanner;

class Gop{
    private int a = 0;
    private int b = 0;
    
    public void Input(int x,int y){
        a = x;
        b = y;
    }
    public int Goping(){
        return a * b;
    }
        
}

public class Main{
    public static void main(String[] args){
        Gop gop = new Gop();
        
        Scanner sc = new Scanner(System.in);
        
        int x,y,result;
        
        x = sc.nextInt();
        y = sc.nextInt();
        
        gop.Input(x,y);
        result = gop.Goping();
        
        System.out.printf("%d\n",result);
    }
}

1-5, 1-6 과 유사함.

 

1-8

import java.util.Scanner;

class Division{
    private double a = 0;
    private double b = 0;
    
    public void Input(double x,double y){
        a = x;
        b = y;
    }
    public double Divi(){
           return a / b; 
    }
        
}

public class Main{
    public static void main(String[] args){
        Division di = new Division();
        
        Scanner sc = new Scanner(System.in);
        
        double x,y;
        double result;
        
        x = sc.nextDouble();
        y = sc.nextDouble();
        
        di.Input(x,y);
        result = di.Divi();
        
        System.out.printf("%.9f\n",result);
    }
}

여기서 좀 틀린 사람이 있을거라 생각합니다. 문제의 조건 중에

라는 문구가 있는데 여기서

System.out.printf("%f\n",result);

라고 하면 오답 처리를 하게 됩니다.

 

문제의 조건대로 출력해 주어야 합니다.

 

1-9

import java.util.Scanner;

class Calculation{
    private int a = 0;
    private int b = 0;
    
    public void Input(int x,int y){
        a = x;
        b = y;
    }
    public int Sum(){
        return a + b;
    }
    public int Subtract(){
        return a - b;
    }
    public int Multiplicate(){
        return a * b;
    }    
    public int Divide(){
        return a / b;
    }
    public int Remainder(){
        return a % b;
    }
}

public class Main{
    public static void main(String[] args){
        Calculation cal = new Calculation();
        
        Scanner sc = new Scanner(System.in);
        
        int x,y;
        
        x = sc.nextInt();
        y = sc.nextInt();
        
        cal.Input(x,y);
        
        System.out.printf("%d\n",cal.Sum());
        System.out.printf("%d\n",cal.Subtract());
        System.out.printf("%d\n",cal.Multiplicate());
        System.out.printf("%d\n",cal.Divide());
        System.out.printf("%d\n",cal.Remainder());
    }
}

위에서 보던 사칙연산의 종합이라고 생각하면 됩니다.

 

 

1-10

import java.util.Scanner;

class Calculation{
    private int a = 0;
    private int b = 0;
    private int c = 0;
    
    public void Input(int x,int y,int z){
        a = x;
        b = y;
        c = z;
    }
    public int Cal1(){
        return (a + b) % c;
    }
    public int Cal2(){
        return ((a % c) + (b % c)) % c;
    }
    public int Cal3(){
        return (a * b) % c;
    }    
    public int Cal4(){
        return ((a % c) * (b % c)) % c;
    }
}

public class Main{
    public static void main(String[] args){
        Calculation cal = new Calculation();
        
        Scanner sc = new Scanner(System.in);
        
        int x,y,z;
        
        x = sc.nextInt();
        y = sc.nextInt();
        z = sc.nextInt();
        
        cal.Input(x,y,z);
        
        System.out.printf("%d\n",cal.Cal1());
        System.out.printf("%d\n",cal.Cal2());
        System.out.printf("%d\n",cal.Cal3());
        System.out.printf("%d\n",cal.Cal4());
    }
}

나머지를 구하기 위해서는 %를 사용해야 합니다.

 

1-11

import java.util.Scanner;

class Calculation{
    private int a = 0;
    private int b = 0;
    private int hundred = 0;
    private int ten = 0;
    private int one = 0;
    
    public void Input(int x,int y){
        a = x;
        b = y;
    }
    
    public void precal(){ // 나눠지는 수의 자릿수를 구하는 작업
        hundred = b / 100;
        ten = (b % 100) / 10;
        one = b % 10;
    }
    
    public int Line1(){
        return a * one;
    }
    public int Line2(){
        return a * ten;
    }
    public int Line3(){
        return a * hundred;
    }    
    public int Results(){
        return a * b;
    }
}

public class Main{
    public static void main(String[] args){
        Calculation cal = new Calculation();
        
        Scanner sc = new Scanner(System.in);
        
        int x,y;
        
        x = sc.nextInt();
        y = sc.nextInt();
        
        cal.Input(x,y);
        
        cal.precal();
        
        System.out.printf("%d\n",cal.Line1());
        System.out.printf("%d\n",cal.Line2());
        System.out.printf("%d\n",cal.Line3());
        System.out.printf("%d\n",cal.Results());
    }
}

precal을 통해서 각 자리에 들어갈 수를 구한 다음에 한 자리씩 숫자를 곱해서 조건에 맞는 수들을 구해주면 됩니다.

 

 


여기까지가 백준코드 단계별로 풀어보기 1단계 풀이입니다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'코딩 > JAVA Baek joon Code' 카테고리의 다른 글

JAVA - Baek joon Code [2]  (0) 2021.03.12
JAVA - Baek joon Code [0]  (0) 2021.03.05