알고리즘 문제/BOJ
6378번 디지털 루트
parkit
2018. 12. 3. 17:57
728x90
반응형
https://www.acmicpc.net/problem/6378
24 ~ 25번 째 코드 실수 주의.
왜 안 되나 몇 분 동안 계속 봤더니, 초보적인 실수를 하였다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; int digit(string s) { int sum = 0; for (int i = 0; i < s.length(); i++) { sum += s[i] - '0'; } if (sum < 10) return sum; else return digit(to_string(sum)); // else digit(to_string(sum)); 이 아니다. 주의하자. } int main(void) { string s; while (1) { cin >> s; if (s[0] == '0') break; cout << digit(s) << '\n'; } return 0; } | cs |
728x90
반응형