๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๋ฐฑ์ค€

[๋ฐฑ์ค€/Java] ๋‹จ๊ณ„๋ณ„๋กœ ํ’€์–ด๋ณด๊ธฐ 5๋‹จ๊ณ„ ๋ฌธ์ž์—ด (27866 ๋ฌธ์ž์™€ ๋ฌธ์ž์—ด, 2743 ๋‹จ์–ด ๊ธธ์ด ์žฌ๊ธฐ, 9086 ๋ฌธ์ž์—ด, 11654 ์•„์Šคํ‚ค ์ฝ”๋“œ, 11720 ์ˆซ์ž์˜ ํ•ฉ, 10809 ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ, 2675 ๋ฌธ์ž์—ด ๋ฐ˜๋ณต, 1152 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜, 2908 ์ƒ์ˆ˜, 5622 ๋‹ค์ด์–ผ, 11718 ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ)

by ๋ฝ€์งœ๊ผฌ 2025. 1. 21.
728x90
๋ฐ˜์‘ํ˜•

๋ฐฑ์ค€

 

๋ฐฑ์ค€ ๋‹จ๊ณ„๋ณ„๋กœ ํ’€์–ด๋ณด๊ธฐ 5๋‹จ๊ณ„ "๋ฌธ์ž์—ด"

(27866 ๋ฌธ์ž์™€ ๋ฌธ์ž์—ด, 2743 ๋‹จ์–ด ๊ธธ์ด ์žฌ๊ธฐ, 9086 ๋ฌธ์ž์—ด, 11654 ์•„์Šคํ‚ค ์ฝ”๋“œ, 11720 ์ˆซ์ž์˜ ํ•ฉ, 10809 ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ, 2675 ๋ฌธ์ž์—ด ๋ฐ˜๋ณต, 1152 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜, 2908 ์ƒ์ˆ˜, 5622 ๋‹ค์ด์–ผ, 11718 ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ)

 

๊ทธ๋ ‡๊ฒŒ ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์—†์—ˆ๋‹ค!

๊ทธ๋ž˜์„œ ๋ชจ๋“  ๋ฌธ์ œ๋“ค์„ ํ•œ๋ฒˆ์— ํฌ์ŠคํŒ…ํ•œ๋‹ค.


27866 ๋ฌธ์ž์™€ ๋ฌธ์ž์—ด (104ms)


๋ฌธ์ œ

๋‹จ์–ด S์™€ ์ •์ˆ˜ i๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, S์˜ i๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์˜์–ด ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 1000์ด๋‹ค.

๋‘˜์งธ ์ค„์— ์ •์ˆ˜ i๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

Sprout
3

์ถœ๋ ฅ

โ€Š์˜ i๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

r

charAt์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๋‹ค.

charAt : ~๋ฒˆ์งธ ๋ฌธ์ž ๋ฐ˜ํ™˜

String str1 = "๋„˜์–ด์ง„ ๊ฑด๋ฌผ ์† ๋ถˆ๋น›๋“ค์€ ์ € ํ˜ธ์ˆ˜์†์— ๊ฐ€๋“ ๋‹ด๊ฒจ์žˆ์–ด";
char ch1 = str1.charAt(0);

// + ๋งˆ์ง€๋ง‰ ๋ฌธ์ž ์–ป๊ธฐ
char ch2 = str1.charAt(str1.length() - 1);

 

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine();
        int i = Integer.parseInt(br.readLine());

        System.out.println(S.charAt(i-1));
    }
}

2743 ๋‹จ์–ด ๊ธธ์ด ์žฌ๊ธฐ (104ms)


๋ฌธ์ œ

 

์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ๊ทธ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์˜์–ด ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 100์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๋‹จ์–ด์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


length๋กœ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine();
        System.out.println(S.length());
    }
}

๋ฐฑ์ข…์›๊ณผ๋ฐฑ์ค€์˜์ƒ๊ด€๊ด€๊ณ„
์‰ฝ์ฅฌ?


9086 ๋ฌธ์ž์—ด (120ms)


๋ฌธ์ œ

๋ฌธ์ž์—ด์„ ์ž…๋ ฅ์œผ๋กœ ์ฃผ๋ฉด ๋ฌธ์ž์—ด์˜ ์ฒซ ๊ธ€์ž์™€ ๋งˆ์ง€๋ง‰ ๊ธ€์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ž…๋ ฅ์˜ ์ฒซ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T(1 ≤ T ≤ 10)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„์— ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ A~Z ๋Œ€๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง€๋ฉฐ ์•ŒํŒŒ๋ฒณ ์‚ฌ์ด์— ๊ณต๋ฐฑ์€ ์—†์œผ๋ฉฐ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1000๋ณด๋‹ค ์ž‘๋‹ค.

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ์ฒซ ๊ธ€์ž์™€ ๋งˆ์ง€๋ง‰ ๊ธ€์ž๋ฅผ ์—ฐ์†ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.


charAt ์‚ฌ์šฉํ•˜์˜€๋‹ค.

์ถœ๋ ฅ์‹œ ""์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์ž๋™ ๋ฌธ์ž์—ด๋กœ ํ˜•๋ณ€ํ™˜๋œ๋‹ค.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());

        for (int i = 0; i < T; i++) {
            String s = br.readLine();
            System.out.println(s.charAt(0) +""+ s.charAt(s.length()-1));
        }
    }
}

11654 ์•„์Šคํ‚ค ์ฝ”๋“œ (104ms)


๋„ˆ๋ฌด ์‰ฌ์›Œ์„œ ๋ฌธ์ œ ์ƒ๋žต.

์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ์ˆซ์ž 0-9์ค‘ ํ•˜๋‚˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฃผ์–ด์ง„ ๊ธ€์ž์˜ ์•„์Šคํ‚ค ์ฝ”๋“œ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž๋™ ํ˜•๋ณ€ํ™˜์„ ํ†ตํ•ด ์กฐ๊ธˆ ํŠน์ดํ•˜๊ฒŒ ํ’€์–ด๋ณด์•˜๋‹ค.

๋’ค์— 0์„ ๋”ํ•ด์„œ int๋กœ ๋งŒ๋“ค์–ด ์ถœ๋ ฅ. ์ด๋Ÿฌ๋ฉด ์•„์Šคํ‚ค์ฝ”๋“œ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String T = br.readLine();

        System.out.println(T.charAt(0)+0);
    }
}

11720 ์ˆซ์ž์˜ ํ•ฉ (100ms)


๋ฌธ์ œ

N๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๊ณต๋ฐฑ ์—†์ด ์“ฐ์—ฌ์žˆ๋‹ค. ์ด ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ํ•ฉํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ˆซ์ž N๊ฐœ๊ฐ€ ๊ณต๋ฐฑ์—†์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ˆซ์ž N๊ฐœ์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.


charAt์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์•„์Šคํ‚ค์ฝ”๋“œ์˜ ํŠน์ง•์„ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

๊ทธ๋ƒฅ ์•„์Šคํ‚ค์ฝ”๋“œ - 48 (๋นผ๊ธฐ 48)ํ•ด์ฃผ๋ฉด ๊ทธ๊ฒŒ ๊ทธ ์ˆ˜์˜ ๊ฐ’์ด๋ฏ€๋กœ

์ด๋ ‡๊ฒŒ ํ•ด์„œ ๋”ํ•ด๋ถ€๋ €๋‹ค.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        String num = br.readLine();

        int sum = 0;
        for (int i = 0; i < N; i++) {
            sum += num.charAt(i) - 48;
        }
        System.out.println(sum);
    }
}
for (int i = 0; i < N; i++) {
		sum += num.charAt(i) - '0';
}

 

48์„ ๋นผ๋Š” ๋ฐฉ๋ฒ• ๋ง๊ณ  0์„ ๋นผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

 

+ ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ• (104ms)

๊ทธ๋ƒฅ ํ•˜๋‚˜ ํ•˜๋‚˜ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ชผ๊ฐœ๊ณ  char → string → int๋กœ ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        String num = br.readLine();

        int sum = 0;
        for (int i = 0; i < N; i++) {
            // Integer.parseInt๋Š” String type์„ ์ธ์ž๋กœ ๋ฐ›์•„์•ผํ•จ.
            sum += Integer.parseInt(String.valueOf(num.charAt(i)));
        }
        System.out.println(sum);
    }
}

 

10809 ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ(124ms)
- indexOf()


๋ฌธ์ œ

์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ ์•ŒํŒŒ๋ฒณ์— ๋Œ€ํ•ด์„œ, ๋‹จ์–ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜๋ฅผ, ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์œผ๋ฉฐ, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์ถœ๋ ฅ

๊ฐ๊ฐ์˜ ์•ŒํŒŒ๋ฒณ์— ๋Œ€ํ•ด์„œ, a๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜, b๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜, ... z๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

๋งŒ์•ฝ, ์–ด๋–ค ์•ŒํŒŒ๋ฒณ์ด ๋‹จ์–ด์— ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹จ์–ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 0๋ฒˆ์งธ ์œ„์น˜์ด๊ณ , ๋‘ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 1๋ฒˆ์งธ ์œ„์น˜์ด๋‹ค.


์•„์Šคํ‚ค์ฝ”๋“œ์˜ ํŠน์„ฑ, ๊ทธ๋ฆฌ๊ณ  indexOf ๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.

indexOf/lastIndexOf : ์ผ์น˜ํ•˜๋Š” ์ฒซ/๋งˆ์ง€๋ง‰ ๋ฌธ์ž์—ด์˜ ์œ„์น˜

=> ์•ž์—์„œ๋ถ€ํ„ฐ ์นด์šดํŠธํ•ด์„œ int๋กœ ๋ฐ˜ํ™˜

String str2 = "์–„๋ฆฌ ์–„๋ฆฌ ์–„๋ผ์…ฉ ์–„๋ผ๋ฆฌ ์–„๋ผ";
int int1 = str2.indexOf('์–„');
int int2 = str2.indexOf('์–„', 4); // ๋‘๋ฒˆ์งธ ์ธ์ž : ~๋ฒˆ์งธ ์ดํ›„/์ด์ „๋ถ€ํ„ฐ ์…ˆ


 

indexOf๋Š” ํฌํ•จ๋˜์ง€ ์•Š์€ ๋ฌธ์ž๋Š” -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•œ ์กฐ๊ฑด์ด๋ž‘ ๋˜‘๊ฐ™๋‹ค.

๊ทธ๋ƒฅ ๋Œ€๋†“๊ณ  ์ด๊ฑธ ์‚ฌ์šฉํ•˜๋ผ๋Š” ๋ฌธ์ œ.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine();
        char a = 'a';

        for (int i = 0; i < 26; i++) {
            System.out.print(S.indexOf(a) + " ");
            a ++;
        }
    }
}

2675 ๋ฌธ์ž์—ด ๋ฐ˜๋ณต (100ms)


๋ฌธ์ œ

๋ฌธ์ž์—ด S๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„์—, ๊ฐ ๋ฌธ์ž๋ฅผ R๋ฒˆ ๋ฐ˜๋ณตํ•ด ์ƒˆ ๋ฌธ์ž์—ด P๋ฅผ ๋งŒ๋“  ํ›„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ฆ‰, ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ R๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ R๋ฒˆ ๋ฐ˜๋ณตํ•˜๋Š” ์‹์œผ๋กœ P๋ฅผ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค. S์—๋Š” QR Code "alphanumeric" ๋ฌธ์ž๋งŒ ๋“ค์–ด์žˆ๋‹ค.

QR Code "alphanumeric" ๋ฌธ์ž๋Š” 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\$%*+-./: ์ด๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T(1 ≤ T ≤ 1,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋ฐ˜๋ณต ํšŸ์ˆ˜ R(1 ≤ R ≤ 8), ๋ฌธ์ž์—ด S๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. S์˜ ๊ธธ์ด๋Š” ์ ์–ด๋„ 1์ด๋ฉฐ, 20๊ธ€์ž๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค. 

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด P๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๊ทธ๋ƒฅ 3์ด๋ž‘ ABC์ž…๋ ฅํ•˜๋ฉด

AAABBBCCC์ด๋ ‡๊ฒŒ ์ถœ๋ ฅ๋˜๊ฒŒ ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();

        for (int t = 0; t < T; t++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int R = Integer.parseInt(st.nextToken());
            String S = st.nextToken();

            for (int i = 0; i < S.length(); i++) {
                for (int j = 0; j < R; j++) {
                    sb.append(S.charAt(i));
                }
            }
            sb.append("\\n");
        }
        System.out.println(sb.toString());;
    }
}

 

์ง€๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๊ตณ์ด ์ €๋ ‡๊ฒŒ ์•ˆํ’€๊ณ  ๋ฌธ์ž์—ด ๋ฐ˜๋ณต ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•ด๋„ ๋ ๋“ฏํ•˜๋‹ค.


๐ŸŽ€ 1152 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ (272ms)
- split


๋ฌธ์ œ

์˜์–ด ๋Œ€์†Œ๋ฌธ์ž์™€ ๊ณต๋ฐฑ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์—๋Š” ๋ช‡ ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์„๊นŒ? ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ํ•œ ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋“ฑ์žฅํ•˜๋ฉด ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ชจ๋‘ ์„ธ์–ด์•ผ ํ•œ๋‹ค.

์ž…๋ ฅ

์ฒซ ์ค„์— ์˜์–ด ๋Œ€์†Œ๋ฌธ์ž์™€ ๊ณต๋ฐฑ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1,000,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๋‹จ์–ด๋Š” ๊ณต๋ฐฑ ํ•œ ๊ฐœ๋กœ ๊ตฌ๋ถ„๋˜๋ฉฐ, ๊ณต๋ฐฑ์ด ์—ฐ์†ํ•ด์„œ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. ๋˜ํ•œ ๋ฌธ์ž์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋๋‚  ์ˆ˜ ์žˆ๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ธ ์ค„ ์•Œ์•˜๋Š”๋ฐ ์˜์™ธ์˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์—ˆ๋˜ ๋ฌธ์ œ.

์šฐ์„  split์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

split : ์ฃผ์–ด์ง„ ๊ธฐ์ค€์œผ๋กœ (~๊ฐœ๊นŒ์ง€) ๋ถ„ํ• ํ•˜์—ฌ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜

๋‚˜๋Š” ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ splitํ–ˆ๋‹ค.
	String str1 = "๊ฐ€๋‚˜๋‹ค๋ผ๋งˆ";
    String str2 = "010-1234-5678";
    String str3 = "ํ•˜๋‚˜ ๋‘˜ ์…‹ ๋„ท ๋‹ค์„ฏ";

    //  ๐Ÿ’ก toCharArray : ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•˜์—ฌ ๋ฌธ์ž์˜ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
    char[] chAry1 = str1.toCharArray();
    char[] chAry2 = str3
            .replace(" ", "")
            .toCharArray();

    //  ๐Ÿ’ก split : ์ฃผ์–ด์ง„ ๊ธฐ์ค€์œผ๋กœ (~๊ฐœ๊นŒ์ง€) ๋ถ„ํ• ํ•˜์—ฌ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
    String[] strAry1 = str1.split("");
    String[] strAry2 = str2.split("-");
    String[] strAry3 = str3.split(" ");
    String[] strAry4 = str3.split(" ", 3);

๐Ÿ“ํ‹€๋ฆฐ ์ฝ”๋“œ

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine().trim();

        String[] strAry = S.split(" ");

        System.out.println(strAry.length);
    }
}

 

์™œ ํ‹€๋ ธ๋ƒ. ํ•˜๋ฉด ํ•˜๋‚˜์˜ ๊ฒฝ์šฐ๋ฅผ ๋นผ๋จน์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋ฐ”๋กœ "๊ทธ๋ƒฅ ๊ณต๋ฐฑ๋งŒ ์ž…๋ ฅ๋˜์—ˆ์„ ๊ฒฝ์šฐ"

๊ทธ๋ƒฅ ๊ณต๋ฐฑ๋งŒ ์ž…๋ ฅ๋˜์—ˆ์„๋•Œ ๊ฐ’์ด 1์ด ๋‚˜์™€์„œ ํ‹€๋ฆฌ๋Š”๊ฒƒ์ด๋‹ค

 

๐Ÿ“์ƒˆ๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine().trim();

        if (S.isEmpty()){
            System.out.println(0);
        }
        else {
            String[] strAry = S.split(" ");

            System.out.println(strAry.length);
        }
    }
}

 

isEmpty() ⇒ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ 0์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

“     ” ์œผ๋กœ ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์— trim()์„ ์‚ฌ์šฉํ–ˆ์„๋•Œ ๊ฒฐ๊ณผ๋Š” ๋นˆ ๋ฌธ์ž์—ด์ด ๋œ๋‹ค.

๋”ฐ๋ผ์„œ isEmpty()๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

 

๊ทธ๋ƒฅ token์œผ๋กœ ํ•ด์„œ ๊ฐœ์ˆ˜ ์ถœ๋ ฅํ–ˆ์–ด๋„ ๋์„๋“ฏ.


2908 ์ƒ์ˆ˜ (100ms)


๋„ˆ๋ฌด ์‰ฌ์›Œ์„œ ๋ฌธ์ œ ์ƒ๋žต.

์ž…๋ ฅ๋ฐ›์€ ์ƒ์ˆ˜๋ฅผ ๋’ค์ง‘์–ด์„œ ๊ฐ’์„ ๋น„๊ตํ•ด ํฐ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        // StringBuilder๋Š” String ํƒ€์ž…์„ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. ๋‹ค๋ฅธ๊ฑด ์•ˆ๋จ.
        StringBuilder A = new StringBuilder(st.nextToken());
        StringBuilder B = new StringBuilder(st.nextToken());
        int a = Integer.parseInt(A.reverse().toString());
        int b = Integer.parseInt(B.reverse().toString());

        if(a>=b){
            System.out.println(a);
        }else {
            System.out.println(b);
        }
    }
}

 

StringBuilder์˜ reverse๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„ ๋’ค์ง‘๊ณ , ์ด๋ฅผ int๋กœ ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ ํ’€์–ด์ฃผ์—ˆ๋‹ค.


5622 ๋‹ค์ด์–ผ (104ms)
- contains


์ด๊ฒƒ๋„ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋ผ ๋ฌธ์ œ๋ฅผ ์ƒ๋žตํ•˜๊ณ , ๋ฌธ์ œ ์š”์•ฝ์œผ๋กœ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

 

 

 

์ˆซ์ž 1์„ ๊ฑธ๋ ค๋ฉด ์ด 2์ดˆ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ( = ์ดˆ๊ธฐ๊ฐ’์ด 2์ดˆ)

1๋ณด๋‹ค ํฐ ์ˆ˜๋ฅผ ๊ฑฐ๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ ์ด๋ณด๋‹ค ๋” ๊ฑธ๋ฆฌ๋ฉฐ,

ํ•œ ์นธ ์˜†์— ์žˆ๋Š” ์ˆซ์ž๋ฅผ ๊ฑธ๊ธฐ ์œ„ํ•ด์„  1์ดˆ์”ฉ ๋” ๊ฑธ๋ฆฐ๋‹ค. ( = 2๋Š” 3์ดˆ๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค๋Š” ๋œป)

๊ทธ๋Ÿฌ๋‚˜, ์ „ํ™”๋Š” ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ํ†ตํ•ด ๊ฑธ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ์•ŒํŒŒ๋ฒณ์„ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋ฐ›๋Š”๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, UNUCIC๋Š” 868242์™€ ๊ฐ™๋‹ค.

 

์ „ํ™”๋ฅผ ๊ฑธ๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ์ตœ์†Œ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ.


๋ฐฐ์—ด์˜ ๊ฐ ์ธ๋ฑ์Šค๋ฅผ “์‹œ๊ฐ„”์œผ๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด ์‰ฝ์ง€ ์•Š์„๊นŒ ํ•˜๊ณ  ์ ‘๊ทผํ•ด๋ณธ ๋ฌธ์ œ.

 

๊ฐ ์ˆซ์ž๋“ค์—๋Š” ์•ŒํŒŒ๋ฒณ์ด ํ• ๋‹น๋˜์–ด์žˆ๋‹ค.

๊ทธ ์ˆซ์ž๋ฅผ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

์ด๊ฒŒ ์ „ํ™”๊ธฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

๊ทธ๋ž˜์„œ alphaNum์ด๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.

 

๊ทธ ํ›„, ๊ฐ ํ• ๋‹น๋œ ์ˆซ์ž๋“ค์— ๋งž๊ฒŒ ์•ŒํŒŒ๋ฒณ๋“ค์„ ๋„ฃ์–ด์ค€๋‹ค.

 

___

๊ทธ๋ฆฌ๊ณ  ์ด ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ

์ฃผ์–ด์ง„ ์•ŒํŒŒ๋ฒณ์ด ์–ด๋Š ์œ„์น˜์— ์žˆ๋Š”์ง€ contains๋ฅผ ํ†ตํ•ด ํ™•์ธํ•œ๋‹ค.

String str_b1 = "์˜›๋‚ ์— ํ˜ธ๋ž‘์ด๊ฐ€ ํ•œ ๋งˆ๋ฆฌ ์‚ด์•˜์–ด์š”.";

//  ๐Ÿ’ก contains : ํฌํ•จ ์—ฌ๋ถ€
boolean bool_b1 = str_b1.contains("ํ˜ธ๋ž‘์ด");
boolean bool_b2 = str_b1.contains("๋‚˜๋ฌด๊พผ");

 

 

๊ทธ๋ฆฌ๊ณ  ๊ทธ ์•ŒํŒŒ๋ฒณ์ด ํฌํ•จ๋˜์–ด์žˆ๋Š”๊ณณ์˜ ์‹œ๊ฐ„์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค.

 

์‹œ๊ฐ„์€ ์ธ๋ฑ์Šค์— ์ดˆ๊ธฐ๊ฐ’์ธ 2๋ฅผ ๋”ํ•ด์„œ ๊ตฌํ•˜์˜€๋‹ค.

0๋ฒˆ์ธ๋ฑ์Šค (=์ˆซ์ž 1๋ฒˆ)์˜ ์‹œ๊ฐ„์€ 2์ดˆ์ด๋ฏ€๋กœ.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        String[] alphaNum = {"","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ",""};
        int sum = 0;
        for (int i = 0; i < input.length(); i++) {
            String c = String.valueOf(input.charAt(i));
            for (int j = 0; j < alphaNum.length; j++) {
                if (alphaNum[j].contains(c)) {
                    sum += j+2;
                }
            }
        }
        System.out.println(sum);
    }
}

 

๊ทธ๋ฆฌํ•˜์—ฌ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ์ด๋ ‡๋‹ค.


11718 ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ (108ms)


๋ง ๊ทธ๋Œ€๋กœ ์ž…๋ ฅ๋ฐ›์€ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

์ €๋ฒˆ์— 10952๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ•œ EOF๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        String str="";

        while((str = br.readLine()) != null){
            bw.write(str + "\\n");
        }
        bw.flush();
    }
}

 

์ˆ˜์›”ํ•˜๊ฒŒ ํ†ต๊ณผ์™„๋ฃŒ

 

728x90
๋ฐ˜์‘ํ˜•