-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6378.cpp
More file actions
56 lines (54 loc) · 863 Bytes
/
6378.cpp
File metadata and controls
56 lines (54 loc) · 863 Bytes
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 6378. 디지털 루트
// 2019.05.21
// 입문용
#include<iostream>
#include<string>
using namespace std;
// 디지털 루트를 만드는 함수
int MakeRoot(int n)
{
int ans = 0;
while (n > 0)
{
ans += n % 10;
n /= 10;
}
// 10보다 작다면 그 값이 디지털 루트
if (ans < 10)
{
return ans;
}
// 아니라면 다시 디지털 루트를 구함
else
{
MakeRoot(ans);
}
}
int main()
{
while (1)
{
// 수가 최대 1000자리라서 string으로 받는다.
string s;
cin >> s;
if (s[0] - '0' == 0)
{
break;
}
int ans = 0;
for (int i = 0; i < s.size(); i++)
{
ans += s[i] - '0';
}
// 처음 한번 했을때 디지털루트가 완성됬다면 출력하고 아니면 만드는 함수 실행
if (ans < 10)
{
cout << ans << endl;
}
else
{
cout << MakeRoot(ans) << endl;
}
}
return 0;
}