"AAAABBBBCCCC"를 뒤집어 "CCCCBBBBAAAA"로 출력하게 하려면 StringBuilder.reverse()를 사용해주면 된다.

 

사용방법

public class Main{
    public static void main(String args[]){
       String a = "AAAABBBBCCCC";
       StringBuilder sb = new StringBuilder(a);
       String result = sb.reverse().toString();
       System.out.println(result);
    }
}

 

 

[풀이1]

import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        boolean[] result = new boolean[42];
        int count = 0;
        for(int a = 0; a < 10; a++){
            int mod = Integer.parseInt(br.readLine())%42;
            if(!result[mod]){
                result[mod]  = true;
                count ++;
            }
        }

        System.out.println(count);

    }
}

 

 

[풀이2]

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

public class Main{
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        HashSet<Integer> hashSet = new HashSet<>();

        for(int a = 0; a < 10; a++){
            hashSet.add(Integer.parseInt(br.readLine())%42);
        }

       System.out.println(hashSet.size());
    }
}

 

 

 

[Java 자바] HashSet

HashSet 이란HashSet은 Java의 Set 인터페이스를 구현한 클래스 중 하나로, 다음과 같은 특징이 있다.같은 값이 두 번 들어가지 않음저장 순서 유지되지 않음빠른 탐색/추가/삭제 가능null 값을 한 번만

ohgbu88.tistory.com

 

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 10807번 / 개수 세기  (0) 2025.08.05
[백준] 2753번 / 윤년  (0) 2025.08.05
[백준] 1330번 / 두 수 비교하기  (0) 2025.08.05
[백준] 1001번 / A-B  (0) 2025.08.05
[백준] 1546번 / 평균 구하기  (0) 2025.07.29

HashSet 이란

HashSet은 Java의 Set 인터페이스를 구현한 클래스 중 하나로, 다음과 같은 특징이 있다.

  • 같은 값이 두 번 들어가지 않음
  • 저장 순서 유지되지 않음
  • 빠른 탐색/추가/삭제 가능
  • null 값을 한 번만 저장할 수 있음

사용방법

import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();

        set.add("a");
        set.add("b");
        set.add("a"); // 중복 → 저장 안 됨
        set.add("c");

        System.out.println(set); 
        System.out.println("크기: " + set.size()); // 중복 없이 총 몇 개 저장되었는지
        System.out.println("b 존재여부 " + set.contains("b"));
    }
}

 

 

결과

 

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 n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int t = Integer.parseInt(br.readLine());
        int count = 0;
        
        for(int i = 0; i < n; i++){
            if(t == Integer.parseInt(st.nextToken())){
                count++;
            }
        }

        br.close();
        System.out.println(count);

    }
}

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 3052번 / 나머지  (0) 2025.08.07
[백준] 2753번 / 윤년  (0) 2025.08.05
[백준] 1330번 / 두 수 비교하기  (0) 2025.08.05
[백준] 1001번 / A-B  (0) 2025.08.05
[백준] 1546번 / 평균 구하기  (0) 2025.07.29

+ Recent posts