자바 백준 14682번
브론즈 3
https://www.acmicpc.net/problem/14682
14682번: Shifty Sum
Suppose we have a number like 12. Let’s define shifting a number to mean adding a zero at the end. For example, if we shift that number once, we get the number 120. If we shift the number again we get the number 1200. We can shift the number as many time
www.acmicpc.net
문제 보기
분류: 수학, 구현, 사칙연산
문제 풀기
주어진 숫자를 String 으로 입력 받고 문자형 "0" 을 붙여나가기로 하였다.
k 번 만큼 반복문을 실행시켜 초기 숫자에 "0" 을 붙여나가고, 그 숫자를 누적해 더한다.
String 을 int 로 변환하기 위해 Integer.parseInt() 를 사용하고, 반복횟수만큼 초기 숫자 뒤에 "0" 을 연달아 붙이기 위해 repeat() 함수를 사용하였다.
코드 보기
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String n = sc.next();
int k = sc.nextInt();
int shiftySum = Integer.parseInt(n);
for (int i = 1; i <= k; i++) {
shiftySum += Integer.parseInt(n + "0".repeat(i));
}
System.out.print(shiftySum);
}
}
'공부하기 > 백준' 카테고리의 다른 글
[Java] 백준 풀기 2864 - 5와 6의 차이 (0) | 2023.04.23 |
---|---|
[Java] 백준 풀기 2847 - 게임을 만든 동준이 (0) | 2023.04.22 |
[Java] 백준 풀기 17201 - 자석 체인 (0) | 2023.04.20 |
[Java] 백준 풀기 26209 - Intercepting Information (0) | 2023.04.19 |
[Java] 백준 풀기 17010 - Time to Decompress (0) | 2023.04.18 |