https://www.acmicpc.net/problem/1110
1110번: 더하기 사이클
0보다 크거나 같고, 99보다 작거나 같은 정수가 주어질 때 다음과 같은 연산을 할 수 있다. 먼저 주어진 수가 10보다 작다면 앞에 0을 붙여 두 자리 수로 만들고, 각 자리의 숫자를 더한다. 그 다음,
www.acmicpc.net
풀고나니 굳이 스트링으로 바꿀 필요가 없다는 것을 깨달았다. 수정은 하지 않았다.
import java.util.*;
import java.io.*;
public class Main
{
public static int calc(int input){
StringBuilder sb = new StringBuilder();
if(input<10){
sb.append(Integer.toString(input));
sb.append(Integer.toString(input));
}else{
sb.append(Integer.toString(input%10));
sb.append(Integer.toString((input/10+input%10)%10));
}
return Integer.valueOf(sb.toString());
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input= sc.nextInt();
int count = 0;
int num=input;
while(true){
num=calc(num);
count++;
if(num==input){
System.out.println(count);
break;
}
}
}
}
'Problem Solving > 구현' 카테고리의 다른 글
BOJ1120 문자열 (2) | 2024.02.01 |
---|---|
BOJ4673 셀프넘버 (0) | 2024.02.01 |
BOJ2231 분해합 (0) | 2024.02.01 |
BOJ1233 주사위 (1) | 2024.02.01 |
BOJ2745 진법 변환 (0) | 2024.02.01 |
https://www.acmicpc.net/problem/1110
1110번: 더하기 사이클
0보다 크거나 같고, 99보다 작거나 같은 정수가 주어질 때 다음과 같은 연산을 할 수 있다. 먼저 주어진 수가 10보다 작다면 앞에 0을 붙여 두 자리 수로 만들고, 각 자리의 숫자를 더한다. 그 다음,
www.acmicpc.net
풀고나니 굳이 스트링으로 바꿀 필요가 없다는 것을 깨달았다. 수정은 하지 않았다.
import java.util.*;
import java.io.*;
public class Main
{
public static int calc(int input){
StringBuilder sb = new StringBuilder();
if(input<10){
sb.append(Integer.toString(input));
sb.append(Integer.toString(input));
}else{
sb.append(Integer.toString(input%10));
sb.append(Integer.toString((input/10+input%10)%10));
}
return Integer.valueOf(sb.toString());
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input= sc.nextInt();
int count = 0;
int num=input;
while(true){
num=calc(num);
count++;
if(num==input){
System.out.println(count);
break;
}
}
}
}
'Problem Solving > 구현' 카테고리의 다른 글
BOJ1120 문자열 (2) | 2024.02.01 |
---|---|
BOJ4673 셀프넘버 (0) | 2024.02.01 |
BOJ2231 분해합 (0) | 2024.02.01 |
BOJ1233 주사위 (1) | 2024.02.01 |
BOJ2745 진법 변환 (0) | 2024.02.01 |