๐Ÿถ ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒ

@Grinยท January 11, 2025 ยท 2 min read

1. ํด๋ž˜์Šค๋ช…์€ Main,

public class Main {
	
}

์ด์™€๊ฐ™์ด ํด๋ž˜์Šค๋ช…์„ Main์œผ๋กœ ๋‘๊ณ  ์ž‘์„ฑํ•œ๋‹ค

2. Main ํ•จ์ˆ˜์—์„œ ๋ฐ”๋กœ ์ž‘์„ฑ ์‹œ, ๋ชจ๋“ ๊ฒƒ์€ static์œผ๋กœ ์„ ์–ธ ํ›„ ์ž‘์„ฑํ•œ๋‹ค.

public class Main {
	private static int max = 0;
	private static int n, k;
	
	private static void dfs(int cnt, int num) {
	
	}
}

main๋ฌธ ์ž์ฒด๊ฐ€ static ํ•จ์ˆ˜ ์ด๋ฏ€๋กœ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ „์—ญ๋ณ€์ˆ˜ ๋ฐ ๋ชจ๋“  ํ•จ์ˆ˜ ๋˜ํ•œ static ์ด์–ด์•ผ ํ•œ๋‹ค.

3. ์ž…๋ ฅ ๊ฐ’์„ ๋ฐ›์„ ๋• Scanner ๋ณด๋‹จ BufferedReader

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public void solution() throws Exeption {
		 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		 int n = Integer.parseInt(br.readLine());
	}
	
	public static void main(String[] args) throws Exception {
		new Main().solution();
	}
}

Scanner๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋‹ค์Œ ์ž…๋ ฅ๊ฐ’์„ ์ฐพ์„ ๋•Œ ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•ด ์†๋„๊ฐ€ ๋А๋ฆฌ๋‹ค.

4.๋ฌธ์ž์—ด ๊ตฌ๋ถ„

String ๋ฌธ์ž์—ด ๊ตฌ๋ถ„ ์‹œ split()๋ณด๋‹จ StringTokenizer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์†๋„๊ฐ€ ๋” ๋น ๋ฅด๋‹ค.

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

public class Main {
    public void solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int s = Integer.parseInt(st.nextToken());

            for (int j = 0; j < s; j++) {
                int data = Integer.parseInt(st.nextToken());
                System.out.println(data);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new Main().solution();
    }
}

์œ„์™€ ๊ฐ™์ด BufferedReader์™€ StringTokenizer๋กœ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค๋ฉด ๋น ๋ฅด๊ฒŒ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

5. ์ž…๋ ฅ์„ ์œ„ํ•œ ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜๋งŒ

  • System.in์ด ๋“ค์–ด๊ฐ„ ํด๋ž˜์Šค๋Š” ๋‹จ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

6. ์ถœ๋ ฅ ์‹œ System.out.printIn() ๋ณด๋‹ค BufferedWriter๊ฐ€ ๋น ๋ฅด๋‹ค

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

public class Main {
    public void solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine()); // ํ† ํฐํ™”
            int s = Integer.parseInt(st.nextToken());

            for (int j = 0; j < s; j++) {
                int data = Integer.parseInt(st.nextToken());
                bw.write(String.valueOf(data)); // ์ถœ๋ ฅ
                bw.newLine(); // ์ค„๋ฐ”๊ฟˆ            }
        }

        bw.flush(); // ์ถœ๋ ฅ ๋ฒ„ํผ ๋น„์šฐ๊ธฐ
        br.close(); // ์ž์› ํ•ด์ œ
        bw.close(); // ์ž์› ํ•ด์ œ
    }

    public static void main(String[] args) throws Exception {
        new Main().solution();
    }
}
@Grin
๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ