알고리즘/프로그래머스 1단계
자릿수 더하기
이제하네
2024. 6. 22. 01:21
코딩테스트 연습 - 자릿수 더하기 | 프로그래머스 스쿨 (programmers.co.kr)
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
public class Solution {
public int solution(int n) {
int answer = 0;
//toString()를 사용하여 String으로 변환
String k = Integer.toString(n);
// split()를 사용하여 배열로 변환
String[] arr = k.split("");
for(int i=0; i<arr.length;i++){
//String배열에 있는걸 하나씩 꺼내서 다시 int 형으로 변환
answer = answer + Integer.parseInt(arr[i]);
}
return answer;
}
}