Skip to content

Commit af2cf49

Browse files
authored
Merge pull request jsk3342#259 from wang-yurin/main
Solve : 046번 문제 해결
2 parents 544c942 + 4233e2c commit af2cf49

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 문제46 : 각 자리수의 합 2
2+
3+
1부터 20까지의(20을 포함) 모든 숫자를 일렬로 놓고 모든 자릿수의 총 합을 구하세요.
4+
5+
예를 들어 10부터 15까지의 모든 숫자를 일렬로 놓으면 101112131415이고
6+
각 자리의 숫자를 더하면 21입니다. (1+0+1+1+1+2+1+3+1+4+1+5 = 21)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
# 문제46 : 각 자리수의 합 2
3+
4+
1부터 20까지의(20을 포함) 모든 숫자를 일렬로 놓고 모든 자릿수의 총 합을 구하세요.
5+
6+
예를 들어 10부터 15까지의 모든 숫자를 일렬로 놓으면 101112131415이고
7+
각 자리의 숫자를 더하면 21입니다. (1+0+1+1+1+2+1+3+1+4+1+5 = 21)
8+
*/
9+
10+
let arr = [];
11+
let sum = 0;
12+
13+
for (let i = 0; i < 20; i++) {
14+
arr[i] = i + 1;
15+
}
16+
17+
arr.forEach((n) => {
18+
while (n !== 0) {
19+
sum += n % 10;
20+
n = Math.floor(n / 10);
21+
}
22+
});
23+
24+
console.log(sum);

0 commit comments

Comments
 (0)