Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Practice G2 week 12 #74

Merged
merged 13 commits into from
Dec 24, 2023
31 changes: 31 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/A - Hello Recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>

using namespace std;

int sum(deque<int> &dq) {
if (dq.size() == 1)
return dq.front();

int frnt = dq.front();
dq.pop_front();
int ans = frnt + sum(dq);
return ans;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int i = 1; i <= t; ++i) {
cout << "Case " << i << ": ";
int n;
cin >> n;
deque<int> dq(n);
for (int j = 0; j < n; ++j) {
cin >> dq[j];
}
cout << sum(dq) << endl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <bits/stdc++.h>

using namespace std;


int fib(int n) {
if (n <= 1)
return n;

int currFib = fib(n - 1) + fib(n - 2);
return currFib;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
cout << fib(n) << endl;
}
29 changes: 29 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/C - Recursive Digit Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>

using namespace std;


long long sum(string &s) {
if (s.size() == 1)
return s.back() - '0';

long long ans = (s.back() - '0');
s.pop_back();
ans += sum(s);
return ans;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string n;
long long k;
cin >> n >> k;
n = to_string(sum(n) * k);
while (n.size() != 1) {
n = to_string(sum(n));
}
cout << n << endl;

}
21 changes: 21 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/D - Truckloads.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <bits/stdc++.h>

using namespace std;

int numOfTrucks(int sz, int sizeOfTruck) {
if (sz <= sizeOfTruck)
return 1;

int ans = numOfTrucks(sz / 2, sizeOfTruck) + numOfTrucks((sz + 1) / 2, sizeOfTruck);
return ans;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int box, szOfTruck;
while (cin >> box >> szOfTruck) {
cout << numOfTrucks(box, szOfTruck) << endl;
}
}
31 changes: 31 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/E - Orthogonality.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>

using namespace std;

int sum(deque<int> &a, deque<int> &b) {
if (a.size() == 1 and b.size() == 1) {
return a.front() * b.front();
}

int f1 = a.front(), f2 = b.front();
a.pop_front();
b.pop_front();
int ans = (f1 * f2) + sum(a, b);
return ans;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
deque<int> a(n), b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
cout << (sum(a, b) == 0 ? "Yes" : "No") << endl;
}
29 changes: 29 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/F - Magic 3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>

using namespace std;


string solve(deque<pair<int, int>> &dq, int s, int d) {
if (dq.empty()) {
return "No";
}
int x = dq.front().first, y = dq.front().second;
if (x < s and y > d)
return "Yes";

dq.pop_front();
return solve(dq, s, d);
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, s, d;
cin >> n >> s >> d;
deque<pair<int, int>> dq(n);
for (auto &s: dq)
cin >> s.first >> s.second;
cout << solve(dq, s, d) << endl;

}
27 changes: 27 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/G - uNrEaDaBlE sTrInG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>

using namespace std;


string solve(int i, string &s) {
if (i == s.size()) {
return "Yes";
}

if ((i + 1) % 2 == 0 && isupper(s[i])) {//even
return solve(i + 1, s);
} else if ((i + 1) % 2 != 0 && islower(s[i])) {//odd
return solve(i + 1, s);
} else {
return "No";
}
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
cout << solve(0, s) << endl;
}
34 changes: 34 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/H - Play Snuke.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <bits/stdc++.h>

using namespace std;


int minPrice(deque<int> &a, deque<int> &p, deque<int> &x) {
if (a.empty()) {
return INT_MAX;
}

int time = a.front(), price = p.front(), stock = x.front();
a.pop_front();
p.pop_front();
x.pop_front();
if (time < stock) { // found video game
int ans = min(price, minPrice(a, p, x));
return ans;
} else // out of stock
return minPrice(a, p, x);
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
deque<int> a(n), p(n), x(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> p[i] >> x[i];
}
int ans = minPrice(a, p, x);
cout << (ans == INT_MAX ? -1 : ans) << endl;
}
28 changes: 28 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/I - Remove It.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>

using namespace std;


void solve(deque<int> &dq, int x) {
if (dq.empty())
return;

int f = dq.front();
if (f != x)
cout << f << ' ';
dq.pop_front();
solve(dq, x);
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, x;
cin >> n >> x;
deque<int> dq(n);
for (auto &s: dq)
cin >> s;
solve(dq, x);
cout << endl;
}
22 changes: 22 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/J - Round Down.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <bits/stdc++.h>

using namespace std;


void solve(string &x) {
if (x.empty() or x.front() == '.')
return;

cout << x.front();
x.erase(x.begin());
solve(x);
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string x;
cin >> x;
solve(x);
}
21 changes: 21 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/K - Savings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <bits/stdc++.h>

using namespace std;

int solve(int day, long long total, int n) {
total += day;
if (total >= n)
return day;

return solve(day + 1, total, n);
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

int n;
cin >> n;
cout << solve(1, 0, n);
}
49 changes: 49 additions & 0 deletions Level 1 Class 2/Practice G2/Week 12/L - Recursive Queries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <bits/stdc++.h>

using namespace std;

int arr[1000000 + 1][10];

int f(int n) {
int prod = 1;
while (n > 0) {
if (n % 10 == 0) {
n /= 10;
continue;
}
prod = prod * (n % 10);
n /= 10;
}
return prod;
}

int g(int n) {
if (n < 10)
return n;

return g(f(n));
}


int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for (int x = 1; x <= 1000000; ++x) {
arr[x][g(x)] = 1;
}
for (int k = 1; k <= 9; ++k) {
for (int i = 1; i <= 1000000; ++i) {
arr[i][k] += arr[i - 1][k];
}
}

int q;
cin >> q;
while (q--) {
int l, r, k;
cin >> l >> r >> k;
int ans = arr[r][k] - arr[l - 1][k];
cout << ans << endl;
}
}