일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자바
- 회귀
- Gem
- Spring JPA
- java
- CS
- 프로그래머스
- 스포티파이
- modern c++
- SWEA
- Spring
- SECS
- c
- python
- Spotify Api
- SECS-II
- Computer Science
- Baekjoon
- C++
- spring boot
- spotify
- SW Expert Academy
- 백준
- 비트겟
- SECS/GEM
- regression
- programmers
- MYSQL
- 파이썬
- 회원가입
Archives
- Today
- Total
비버놀로지
[Programmers 프로그래머스] 12931 자릿수 더하기 본문
728x90
programmers.co.kr/learn/courses/30/lessons/12931
입력값을 10으로 나누어 주면서 나머지 값을 계속 더해주는 방식으로 계산을 했다. 입력값을 10으로 나눴을때 0이 된다면 while문을 종료하고 출력하게 된다.
import java.util.*;
public class Solution {
public int solution(int n) {
int answer=0;
while(n!=0) {
answer+=n%10;
n/=10;
}
return answer;
}
}
728x90
'ALGORITM > JAVA' 카테고리의 다른 글
[Programmers 프로그래머스] 12933 정수 내림차순으로 배치하기 (0) | 2021.01.08 |
---|---|
[Programmers 프로그래머스] 12932 자연수 뒤집어 배열로 만들기 (0) | 2021.01.08 |
[Programmers 프로그래머스] 12930 이상한 문자 만들기 (0) | 2021.01.08 |
[Programmers 프로그래머스] 12928 약수의 합 (0) | 2021.01.08 |
[Programmers 프로그래머스] 12926 시저 암호 (0) | 2021.01.08 |
Comments