import java.util.*;
public class Solution {
public int solution(int n) {
int answer = 0;
//length를 구하기위해 n을 String으로 형변환
String conv = String.valueOf(n);
//각 n의 값들 구하여 더하기 - 문자열 + for문 사용
for(int i = 0; i < conv.length(); i++)
{
//아스키 코드 값 감산
answer += conv.charAt(i)-48;
//자릿에 맞는 값인지 검산
System.out.println(conv.charAt(i));
//합이 올바른지 검산
System.out.println(answer);
};
System.out.println(answer);
return answer;
}
}
'프로그래밍 > Algorithm' 카테고리의 다른 글
2022년 05월 14일 프로그래머스 - 땅따먹기(다이나믹 프로그래밍, DP) (0) | 2022.05.14 |
---|---|
2022년 05월 13일 프로그래머스 - 가장 큰 정사각형(다이나믹 프로그래밍, DP) (0) | 2022.05.13 |
2022년 05월 12일 프로그래머스 - 나머지 한 점 (0) | 2022.05.12 |
2022년 05월 11일 프로그래머스 - 순열 검사 (0) | 2022.05.11 |
댓글