본문 바로가기
Algorithm

백준 자바 제출 방법

by 이은선 2023. 7. 23.
728x90
SMALL

▪︎ 클래스명은 'Main'으로, 패키지는 없어야 함.

public class Main {
	public static void main(String[] args) {
    	// ...
    }
}

 

▪︎ BufferedReader, BufferedWriter

: Scanner나 sysout보다 속도가 빠르기 때문에 사용

 

1. 입력

// 엔터로 구분하여 입력을 받음.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// 띄어쓰기로 구분하는 경우에는 StringTokenizer 사용
StringTokenizer st = new StringTokenizer(br.readline());

 

2. 출력

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("입력 내용"); // 버퍼에 넣을 내용
bw.flush(); // 내용 출력
bw.close(); // 종료

 

ex)

public class Main {
	public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Buffered Writer bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readline());
        
        while (st.countTokens() != 0)
        	bw.write(st.nextToken()+", ");
            
        bw.flush();
 }

입력 : a s d
출력 : a, s, d,

 

▪︎ Main 이외의 클래스를 추가로 사용시, public이 아닌 클래스 혹은 Inner 클래스 사용

: public class는 하나여야 하고, 무조건 Main이어야 함.

class Pos {
	int x, y;
    public Pos(int x, int y) {
    	this.x = x;
        this.y = y;
    }
 }
 
 public class Main {
 	class Node {
    	int n, dist;
        public Node(int n, int dist) {
        	this.n = n;
            this.dist = dist;
        }
    }
    
    private void Solution() throws Exception {
    	Pos p = new Pos(1, 2);
        Nnode node = new Node(3, 45);
    }

 

▪︎ Main 함수에서 바로 작성시, static으로 작성 필요

: main문 자체가 static 함수이므로 거기서 사용하는 전역변수 및 모든 함수 또한 static이어야 한다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	private static int max = 0;
    private static int n, k;
    private static int[] arr;
    
    private static void dfs(int cnt, int num){
    	if (cnt==8){
        	return;
        }
        
        num *= 10;
        if (num >= n)
        	return;
        
        for (int i=0; i<k; i++){
        	int tmp = num + arr[i];
            if (tmp > n) continue;
            if (tmp > max) max = tmp;
            dfs(cnt+1, tmp);
         }
 	}
    
    public static void main(String[] args) throws Exception{
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());
        k = Integer.parseInt(st.nextToken());
        arr = new int[k];
        st = new StringTokenizer(br.readLine());
        for(int i=0; i<k; i++) arr[i] = Integer.parseInt(st.nextToken());
        
        dfs(0,0);
        System.out.printIn(max);
     }
  }

 

다음과 같이 함수로 한번 감싸주면 된다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	private int max = 0;
    private static int n, k;
    private static int[] arr;
    
    private void dfs(int cnt, int num){
    	if (cnt==8){
        	return;
        }
        
        num *= 10;
        if (num >= n)
        	return;
        
        for (int i=0; i<k; i++){
        	int tmp = num + arr[i];
            if (tmp > n) continue;
            if (tmp > max) max = tmp;
            dfs(cnt+1, tmp);
         }
 	}
    
    public void solution() throws Exception{
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());
        k = Integer.parseInt(st.nextToken());
        arr = new int[k];
        st = new StringTokenizer(br.readLine());
        for(int i=0; i<k; i++) arr[i] = Integer.parseInt(st.nextToken());
        
        dfs(0,0);
        System.out.printIn(max);
     }
     
     public static void main(String[] args) throws Exception{
     	new Main().solution();
     }
  }

 

▪︎ 입력을 위한 클래스는 하나만 쓸 것

728x90
LIST

'Algorithm' 카테고리의 다른 글

[백준] 15649번 : N과 M (1) (파이썬)  (0) 2024.01.10