-
프로그래머스 - 자릿수 더하기 [자바]Algorithms/- 프로그래머스 2022. 1. 31. 22:18
- 문제 링크 : 자릿수 더하기
정답
- 형변환으로 풀고 싶을 때
public class Solution { public int solution(int n) { int answer = 0; String[] arr =String.valueOf(n).split(""); for(String str : arr){ answer += Integer.valueOf(str); } return answer; } }
- 정석적으로 풀고 싶을 때
public class Solution { public int solution(int n) { int answer = 0; int len = (int)Math.log10(n) + 1; for(int i=0; i<len; i++){ answer += n % 10; n /= 10; } return answer; } }
분석
- 단순 사칙연산
반응형