알고리즘/백준
백준-1748번/수 이어 쓰기 1 (java)
연향동큰손
2024. 6. 27. 18:19
문제 해결
변수
1) result = 붙혀쓴 자리 수
2) len = 반복문 안에서 10 , 100, 1000 .... 단위로 넘어가는지를 알 수 있게 해주는 변수
3) count = 반복문 안에서 자릿수에 맞게 result에 더해지는 수
for(int i=1; i<=n; i++){
if(i==len){
len=len*10;
count++;
}
result=result+count;
i가 10, 100, .....단위로 증가 할때마다 더해지는 숫자(count)도 1,2,3...씩 계속 증가해야 하므로 i가 len과 같아지면 len은 10을 곱해주고 더해줘야하는 변수 count도 1 증가
<전체 코드>
import java.util.Scanner;
public class Problem1748 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int result=0;
int len=10;
int count=1;
for(int i=1; i<=n; i++){
if(i==len){
len=len*10;
count++;
}
result=result+count;
}
System.out.println(result);
}
}