백준, 프로그래머스

자연수 뒤집어 더하기

숲별 2022. 9. 27. 14:57
728x90
//모의고사 1번 자연수 뒤집어 더하기
public class Main {
    public String solution(int n) {
        String answer = "";
        String s = Integer.toString(n);
        int sum = 0;
        for (int i=(s.length()-1); i >= 0; i--) {
            answer += s.substring(i, i+1);
            sum +=Integer.parseInt(s.substring(i, i+1));
            if (i>0){
                answer += "+";
            }else {
                answer += "=";
            }
        }

        return answer+sum;

    }
    public static void main(String[] args) {
        Main method = new Main();
        System.out.println(method.solution(718253));
    }
}

=>3+5+2+8+1+7=26

'백준, 프로그래머스' 카테고리의 다른 글

15-내적  (0) 2022.09.28
7-음양 더하기  (0) 2022.09.28
[프로그래머스]문자열을 정수로 바꾸기  (0) 2022.09.24
[프로그래머스] 두 정수 사이의 합  (1) 2022.09.24
[프로그래머스] 짝수와 홀수  (0) 2022.09.24