Skip to content

Commit f593b89

Browse files
Merge branch 'master' of https://github.com/swapnanildutta/Hackerrank-Codes into master
2 parents 62eba60 + c99844d commit f593b89

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

CPP/RecursiveDigitSum.cpp

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
We define super digit of an integer using the following rules:
3+
4+
Given an integer, we need to find the super digit of the integer.
5+
6+
If has only digit, then its super digit is .
7+
Otherwise, the super digit of is equal to the super digit of the sum of the digits of .
8+
For example, the super digit of will be calculated as:
9+
10+
super_digit(9875) 9+8+7+5 = 29
11+
super_digit(29) 2 + 9 = 11
12+
super_digit(11) 1 + 1 = 2
13+
super_digit(2) = 2
14+
You are given two numbers and . The number is created by concatenating the string times. Continuing the above example where , assume your value . Your initial (spaces added for clarity).
15+
16+
superDigit(p) = superDigit(9875987598759875)
17+
5+7+8+9+5+7+8+9+5+7+8+9+5+7+8+9 = 116
18+
superDigit(p) = superDigit(116)
19+
1+1+6 = 8
20+
superDigit(p) = superDigit(8)
21+
All of the digits of sum to . The digits of sum to . is only one digit, so it's the super digit.
22+
23+
Function Description
24+
25+
Complete the function superDigit in the editor below. It must return the calculated super digit as an integer.
26+
27+
superDigit has the following parameter(s):
28+
29+
n: a string representation of an integer
30+
k: an integer, the times to concatenate to make
31+
Input Format
32+
33+
The first line contains two space separated integers, and .
34+
35+
Constraints
36+
37+
Output Format
38+
39+
Return the super digit of , where is created as described above.
40+
41+
Sample Input 0
42+
43+
148 3
44+
Sample Output 0
45+
46+
3
47+
Explanation 0
48+
49+
Here and , so .
50+
51+
super_digit(P) = super_digit(148148148)
52+
= super_digit(1+4+8+1+4+8+1+4+8)
53+
= super_digit(39)
54+
= super_digit(3+9)
55+
= super_digit(12)
56+
= super_digit(1+2)
57+
= super_digit(3)
58+
= 3.
59+
Sample Input 1
60+
61+
9875 4
62+
Sample Output 1
63+
64+
8
65+
Sample Input 2
66+
67+
123 3
68+
Sample Output 2
69+
70+
9
71+
Explanation 2
72+
73+
Here and , so .
74+
75+
super_digit(P) = super_digit(123123123)
76+
= super_digit(1+2+3+1+2+3+1+2+3)
77+
= super_digit(18)
78+
= super_digit(1+8)
79+
= super_digit(9)
80+
= 9
81+
*/
82+
83+
#include <bits/stdc++.h>
84+
85+
using namespace std;
86+
87+
vector<string> split_string(string);
88+
89+
// Complete the superDigit function below.
90+
int superDigit(string n, int k) {
91+
92+
if(n.size() == 1 && k == 1) return n[0]-'0';
93+
94+
long long sum = 0;
95+
for(char c:n)
96+
sum+=(c-'0');
97+
98+
sum*=k;
99+
100+
while(sum > 10){
101+
long long s = 0;
102+
while(sum > 0){
103+
s+=sum%10;
104+
sum/=10;
105+
}
106+
sum = s;
107+
}
108+
109+
return (int)sum;
110+
}
111+
112+
int main()
113+
{
114+
ofstream fout(getenv("OUTPUT_PATH"));
115+
116+
string nk_temp;
117+
getline(cin, nk_temp);
118+
119+
vector<string> nk = split_string(nk_temp);
120+
121+
string n = nk[0];
122+
123+
int k = stoi(nk[1]);
124+
125+
int result = superDigit(n, k);
126+
127+
fout << result << "\n";
128+
129+
fout.close();
130+
131+
return 0;
132+
}
133+
134+
vector<string> split_string(string input_string) {
135+
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
136+
return x == y and x == ' ';
137+
});
138+
139+
input_string.erase(new_end, input_string.end());
140+
141+
while (input_string[input_string.length() - 1] == ' ') {
142+
input_string.pop_back();
143+
}
144+
145+
vector<string> splits;
146+
char delimiter = ' ';
147+
148+
size_t i = 0;
149+
size_t pos = input_string.find(delimiter);
150+
151+
while (pos != string::npos) {
152+
splits.push_back(input_string.substr(i, pos - i));
153+
154+
i = pos + 1;
155+
pos = input_string.find(delimiter, i);
156+
}
157+
158+
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
159+
160+
return splits;
161+
}

0 commit comments

Comments
 (0)