sum-of-digits-in-base-k
1.0.0
Sum of Digits in Base K
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <bits/stdc++.h>
2
3
using namespace
std;
4
5
class
Solution
6
{
7
public
:
8
int
sumBase
(
int
n,
int
k)
9
{
10
if
(k < 2 || k > 10)
11
throw
runtime_error(
"Wrong base"
);
12
13
int
sum = 0;
14
while
(n > 0)
15
{
16
sum += n % k;
17
n /= k;
18
}
19
return
sum;
20
}
21
};
22
23
int
main
()
24
{
25
int
n = 34;
26
int
k = 6;
27
try
28
{
29
cout <<
Solution
().
sumBase
(n, k) << endl;
30
}
31
catch
(
const
std::exception& e)
32
{
33
cerr << e.what() << endl;
34
}
35
return
0;
36
}
Solution
Definition
main.cpp:6
Solution::sumBase
int sumBase(int n, int k)
Definition
main.cpp:8
main
int main()
Definition
main.cpp:23
main.cpp
Generated by
1.9.8