주어진 수의 각 자릿수를 모두 더한 값은?
Bash
bash
read n
read m
#while [ $m != 0 ]; do
# ((t += m%10, m /= 10))
#done
for ((i=0; i<$n; i++)); do
add=${m:$i:1}
((t += add))
done
echo $t
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
주석 처리된 코드는 백준에서 틀린 답이라고 나온다.
Node.js
javascript
const [n, num] = require("fs").readFileSync(0).toString().trim().split("\n");
const arr = num.split("").map(Number);
console.log( arr.reduce((r, x, i) => { return r+x; }) );
1
2
3
2
3
Python3
python
n = int(input())
arr = map(int, list(input()))
print( sum(arr) )
1
2
3
2
3
Ruby
ruby
n = gets.chomp.to_i
arr = gets.chomp.split("").map {|i| i.to_i}
puts arr.sum()
1
2
3
2
3
Comments
Not supported comment edit and upvote
You can do it on this page if you want.