Skip to content

Commit afed4fa

Browse files
committed
Solve Recursive Digit Sum from Hackerrank, CODEM5 from SPOJ, 1466 from URI and 990 from UVA
1 parent 72026e9 commit afed4fa

8 files changed

+259
-0
lines changed

Hackerrank/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
- [Project Euler #10: Summation of primes](https://www.hackerrank.com/contests/projecteuler/challenges/euler010)
4848
- [Project Euler #11: Largest product in a grid](https://www.hackerrank.com/contests/projecteuler/challenges/euler011)
4949
- [Queues: A Tale of Two Stacks](https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks)
50+
- [Recursive Digit Sum](https://www.hackerrank.com/challenges/recursive-digit-sum)
5051
- [Repeated String](https://www.hackerrank.com/challenges/repeated-string)
5152
- [Say "Hello, World!" With C++](https://www.hackerrank.com/challenges/cpp-hello-world)
5253
- [Sherlock and Anagrams](https://www.hackerrank.com/challenges/sherlock-and-anagrams)

Hackerrank/Recursive Digit Sum.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
string n;
7+
int k, res;
8+
9+
int toInt() {
10+
int tmp = 0;
11+
for(int i = 0; i < n.length(); ++i)
12+
tmp += (n[i] - '0');
13+
return tmp;
14+
}
15+
16+
void stringSum() {
17+
int tmp = 0;
18+
for(int i = 0; i < n.length(); ++i)
19+
tmp += (n[i] - '0');
20+
21+
n = "";
22+
while(tmp != 0) {
23+
n += ((tmp % 10) + '0');
24+
tmp /= 10;
25+
}
26+
}
27+
28+
void stringRec() {
29+
stringSum();
30+
if(n.length() == 1) return;
31+
stringRec();
32+
}
33+
34+
void intSum() {
35+
int tmp = 0;
36+
while(res != 0) {
37+
tmp += (res % 10);
38+
res /= 10;
39+
}
40+
res = tmp;
41+
}
42+
43+
void intRec() {
44+
intSum();
45+
if(res < 10) return;
46+
intRec();
47+
}
48+
49+
int main() {
50+
cin >> n >> k;
51+
stringRec();
52+
res = toInt() * k;
53+
intRec();
54+
cout << res << endl;
55+
56+
return 0;
57+
}

SPOJ/CODEM5 - Problem5.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int n, k, a[101];
7+
8+
int rec(int i, int sum) {
9+
if(sum == k)
10+
return 0;
11+
12+
if(i == n)
13+
return 1e5;
14+
15+
return min(rec(i + 1, sum + a[i]) + 1, rec(i + 1, sum));
16+
}
17+
18+
int main() {
19+
int t;
20+
scanf("%d", &t);
21+
while(t-- != 0) {
22+
scanf("%d %d", &n, &k);
23+
for(int i = 0; i < n; ++i)
24+
scanf("%d", a + i);
25+
26+
int res = rec(0, 0);
27+
if(res >= 1e5)
28+
puts("impossible");
29+
else
30+
printf("%d\n", res);
31+
}
32+
33+
return 0;
34+
}

SPOJ/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [BGSHOOT - Shoot and kill](http://www.spoj.com/problems/BGSHOOT/)
77
- [BSEARCH1 - Binary search](http://www.spoj.com/problems/BSEARCH1/)
88
- [CODEIT02 - PICK UP DROP ESCAPE](http://www.spoj.com/problems/CODEIT02/)
9+
- [CODEM5 - Problem5](http://www.spoj.com/problems/CODEM5/)
910
- [CPRIME - Prime Number Theorem](http://www.spoj.com/problems/CPRIME/)
1011
- [CRSCNTRY - Cross-country](http://www.spoj.com/problems/CRSCNTRY/)
1112
- [DCOWS - Dancing Cows](http://www.spoj.com/problems/DCOWS/)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <stdio.h>
2+
#include <memory.h>
3+
#include <queue>
4+
#include <vector>
5+
6+
using namespace std;
7+
8+
struct node {
9+
static node *sentinel;
10+
11+
node *left, *right;
12+
int value;
13+
14+
node() {
15+
memset(this, 0, sizeof *this);
16+
}
17+
18+
node(int value) {
19+
left = right = sentinel;
20+
this->value = value;
21+
}
22+
};
23+
24+
node *node::sentinel = new node;
25+
26+
node * insert(node *cur, int &value) {
27+
if(cur == node::sentinel) return new node(value);
28+
if(cur->value > value) cur->left = insert(cur->left, value);
29+
if(cur->value < value) cur->right = insert(cur->right, value);
30+
return cur;
31+
}
32+
33+
void free_bst(node *cur) {
34+
if(cur == node::sentinel) return;
35+
free_bst(cur->left);
36+
free_bst(cur->right);
37+
delete cur;
38+
}
39+
40+
int n, tmp;
41+
queue<node*> qu;
42+
vector<int> bfs_res;
43+
44+
void BFS(node *cur) {
45+
while(!qu.empty()) qu.pop();
46+
qu.push(cur);
47+
48+
int size;
49+
node *fr;
50+
while(!qu.empty()) {
51+
size = qu.size();
52+
53+
while(size-- != 0) {
54+
fr = qu.front();
55+
qu.pop();
56+
57+
bfs_res.push_back(fr->value);
58+
59+
if(fr->left != node::sentinel)
60+
qu.push(fr->left);
61+
62+
if(fr->right!= node::sentinel)
63+
qu.push(fr->right);
64+
}
65+
}
66+
}
67+
68+
int main() {
69+
node *root;
70+
71+
int t;
72+
scanf("%d", &t);
73+
for(int cnt = 1; t-- != 0; ++cnt) {
74+
bfs_res.clear();
75+
root = NULL;
76+
77+
scanf("%d", &n);
78+
for(int i = 0; i < n; ++i) {
79+
scanf("%d", &tmp);
80+
if(root == NULL)
81+
root = new node(tmp);
82+
else
83+
insert(root, tmp);
84+
}
85+
86+
printf("Case %d:\n", cnt);
87+
BFS(root);
88+
for(int i = 0; i < (int)bfs_res.size(); ++i)
89+
printf("%s%d", (i != 0 ? " " : ""), bfs_res[i]);
90+
puts("\n");
91+
free_bst(root);
92+
}
93+
94+
return 0;
95+
}
96+

URI/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
- [1424 - Easy Problem from Rujia Liu?](https://www.urionlinejudge.com.br/judge/en/problems/view/1424)
7070
- [1430 - Jingle Composing](https://www.urionlinejudge.com.br/judge/en/problems/view/1430)
7171
- [1451 - Broken Keyboard](https://www.urionlinejudge.com.br/judge/en/problems/view/1451)
72+
- [1466 - Level Order Tree Traversal](https://www.urionlinejudge.com.br/judge/en/problems/view/1466)
7273
- [1467 - Zero or One](https://www.urionlinejudge.com.br/judge/en/problems/view/1467)
7374
- [1469 - Boss](https://www.urionlinejudge.com.br/judge/en/problems/view/1469)
7475
- [1548 - Canteen Queue](https://www.urionlinejudge.com.br/judge/en/problems/view/1548)

UVA/990 - Diving for Gold.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
#include <memory.h>
3+
#include <vector>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
int t, w, n, dp[1001][31];
9+
pair<int, int> treasures[31];
10+
vector<int> sol;
11+
12+
int rec(int time, int index) {
13+
if(time < 0)
14+
return -1e5;
15+
16+
if(index == n)
17+
return 0;
18+
19+
int &ret = dp[time][index];
20+
if(ret != -1) return ret;
21+
ret = 0;
22+
23+
int r1, r2;
24+
25+
r1 = rec(time - (3 * w * treasures[index].first), index + 1) + treasures[index].second;
26+
r2 = rec(time, index + 1);
27+
28+
return ret = max(r1, r2);
29+
}
30+
31+
void build_path(int time, int index) {
32+
if(time < 0)
33+
return;
34+
35+
if(index == n)
36+
return;
37+
38+
if(rec(time, index) == rec(time - (3 * w * treasures[index].first), index + 1) + treasures[index].second) {
39+
sol.push_back(index);
40+
build_path(time - (3 * w * treasures[index].first), index + 1);
41+
} else {
42+
build_path(time, index + 1);
43+
}
44+
}
45+
46+
int main() {
47+
bool blankline = false;
48+
while(scanf("%d %d", &t, &w) != EOF) {
49+
if(blankline) puts("");
50+
scanf("%d", &n);
51+
for(int i = 0; i < n; ++i)
52+
scanf("%d %d", &treasures[i].first, &treasures[i].second);
53+
54+
memset(dp, -1, sizeof dp);
55+
printf("%d\n", rec(t, 0));
56+
57+
sol.clear();
58+
build_path(t, 0);
59+
60+
printf("%d\n", (int)sol.size());
61+
for(int i = 0; i < (int)sol.size(); ++i)
62+
printf("%d %d\n", treasures[sol[i]].first, treasures[sol[i]].second);
63+
blankline = true;
64+
}
65+
66+
return 0;
67+
}
68+

UVA/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [836 - Largest Submatrix](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=777)
3939
- [900 - Brick Wall Patterns](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=841)
4040
- [929 - Number Maze](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=870)
41+
- [990 - Diving for Gold](https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=931)
4142
- [1121 - Subsequence](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3562)
4243
- [1124 - Celebrity jeopardy](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3565)
4344
- [1148 - The mysterious X network](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3589)

0 commit comments

Comments
 (0)