View

📚 문제

알파벳 대문자와 숫자(0~9)로만 구성된 문자열이 입력으로 주어진다. 이때 모든 알파벳을 오름차순으로 정렬하여 이어서 출력한 뒤에, 그 뒤에 모든 숫자를 더한 값을 이어 출력한다.

📝 문제 해설

문자열 ➡️ 별도의 리스트에 저장 후 정렬
숫자 ➡️ 합계 계산 후, 문자열 뒤에 합계 출력

💻 코드

package implementation;

import java.util.*;

public class impl06 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        Scanner sc = new Scanner(System.in);
        String str = sc.next();

        ArrayList<Character> result = new ArrayList<Character>();
        int value = 0;

        for (int i = 0; i < str.length(); i++) {
            if (Character.isLetter(str.charAt(i))) {
                result.add(str.charAt(i));
            }
            else {
                value += str.charAt(i) - '0';
            }
        }

        Collections.sort(result);

        for (int i = 0; i < result.size(); i++) {
           sb.append(result.get(i));
        }

        if (value != 0) sb.append(value);
        
        System.out.println(sb);
    }
}
728x90
Share Link
reply
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31