19-> 1+ 9 = 10 ์ด๋ฐ์์ผ๋ก ์๋ฆฟ์๋ฅผ ๊ตฌํด์ฃผ๋ ์ฝ๋์ด๋ค.
#include <iostream>
using namespace std;
int seperateAndPlus(int x) {
int res = 0;
while (x != 0) {
res = res + x % 10;
x /= 10;
}
return res;
}
int main() {
int num;
cin >> num;
cout << seperateAndPlus(num);
}
๊ด๋ จ ๋ฌธ์
https://programmers.co.kr/learn/courses/30/lessons/12931
https://www.acmicpc.net/problem/1110
'์ฝ๋ฉํ ์คํธ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++ ์๊ณ ๋ฆฌ์ฆ] ๋ค์ด๋๋ฏนํ๋ก๊ทธ๋๋ฐ2 - ๋ฐฐ๋ญ๋ฌธ์ (Knapsack Problem) (0) | 2022.01.04 |
---|---|
[C++] ๋ฒกํฐ ํ์ (0) | 2021.09.07 |
[C++ ์๊ณ ๋ฆฌ์ฆ] ๋ถํ ์ ๋ณต(Divide and conquer) (0) | 2021.08.28 |
[C++ ์๊ณ ๋ฆฌ์ฆ] ๊ทธ๋ฆฌ๋(ํ์๋ฒ,Greedy Algorithm) (2) | 2021.08.08 |
[C++ ์๊ณ ๋ฆฌ์ฆ] ๋์ ๊ณํ๋ฒ(Dynamic Programming) (2) | 2021.07.31 |
๋๊ธ