Skip to content

Commit d9766f0

Browse files
committed
Upload all
1 parent d6fb8dc commit d9766f0

24 files changed

+644
-0
lines changed

algo/bezout.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
3+
int main() {
4+
for (int i = -1000; i < 1000; i++) {
5+
for (int j = -1000; j < 1000; j++) {
6+
if (84*i + 62*j == 2) {
7+
std::cout << "i is: " << i <<"j is: " << j << std::endl;
8+
}
9+
}
10+
}
11+
return 0;
12+
}

algo/dfs.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
void dfs(vector< vector<int> >& adj, int s, int* visited) {
7+
if (visited[s]) return;
8+
visited[s] = true;
9+
for (auto it = adj[s].begin(); it != adj[s].end(); ++it) {
10+
dfs(adj, *it, visited);
11+
}
12+
}
13+
int main() {
14+
vector<int> adj[4];
15+
adj[0].push_back(1);
16+
adj[0].push_back(2);
17+
adj[1].push_back(3);
18+
adj[2].push_back(3);
19+
20+
dfs(adj, 0, new int[4]);
21+
return 0;
22+
}

algo/permutations.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
int main() {
7+
vector<int> v = {1,2,3,4,5};
8+
do {
9+
for (auto i : v) {
10+
cout << i << " ";
11+
}
12+
cout << endl;
13+
} while (next_permutation(v.begin(), v.end()));
14+
return 0;
15+
}

algo/powerset.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
void search(int k, vector<int>& subset) {
7+
if (k == 3) {
8+
for (auto v: subset) {
9+
cout << v << " ";
10+
}
11+
cout << endl;
12+
} else {
13+
search(k+1, subset);
14+
subset.push_back(k);
15+
search(k+1, subset);
16+
subset.pop_back();
17+
}
18+
}
19+
int main() {
20+
vector<int> v;
21+
search(0, v);
22+
// Bit Sequence
23+
cout << "Bit Sequence" << endl;
24+
for (int b = 0; b < (1<<3); b++) {
25+
cout << b << endl;
26+
}
27+
return 0;
28+
}

c++/2darr.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
void modify(int* arr) {
6+
arr[2] = 69;
7+
arr[3] = 42;
8+
arr[4] = 1337;
9+
}
10+
11+
void modify2d(int** arr) {
12+
arr[1][1] = 69;
13+
arr[1][2] = 42;
14+
arr[2][1] = 1337;
15+
arr[2][2] = 9001;
16+
}
17+
18+
int main() {
19+
int *a = new int[7];
20+
modify(a);
21+
for (int i = 0; i < 7; i++) {
22+
cout << a[i] << " ";
23+
}
24+
cout << endl;
25+
delete[] a;
26+
27+
// 2d array
28+
int **b = new int*[4];
29+
for (int i = 0; i < 4; i++) {
30+
b[i] = new int[4];
31+
}
32+
modify2d(b);
33+
for (int i = 0; i < 4; i++) {
34+
for (int j = 0; j < 4; j++) {
35+
cout << b[i][j] << " ";
36+
}
37+
cout << endl;
38+
}
39+
cout << endl;
40+
for (int i = 0; i < 4; i++) {
41+
delete[] b[i];
42+
}
43+
delete[] b;
44+
return 0;
45+
}

c++/2dvec.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
void modify(vector< vector<int> >& vec) {
7+
for (int i = 0; i < vec.size(); i++) {
8+
for (int j = 0; j < vec[0].size(); j++) {
9+
vec[i][j] = 5;
10+
}
11+
}
12+
}
13+
int main() {
14+
int n_row = 3, n_col = 4;
15+
vector< vector<int> > v;
16+
v.resize(n_row, vector<int>(n_col));
17+
modify(v);
18+
for (int i = 0; i < n_row; i++) {
19+
for (int j = 0; j < n_col; j++) {
20+
cout << v[i][j] << " ";
21+
}
22+
cout << endl;
23+
}
24+
return 0;
25+
}

c++/complex.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class complex {
6+
double re, im;
7+
public:
8+
complex(double r, double i) :re(r), im(i) {}
9+
complex(double r) :re(r), im(0) {}
10+
complex() :re(0), im(0) {}
11+
double real() const { return re; }
12+
void real(double d) { re=d; }
13+
double imag() const { return im; }
14+
void imag(double d) { im=d; }
15+
complex& operator+=(complex z) {
16+
re+=z.re, im+=z.im; return *this;
17+
}
18+
complex& operator-=(complex z) {
19+
re-=z.re, im-=z.im; return *this;
20+
}
21+
complex& operator*=(complex z);
22+
complex& operator/=(complex z);
23+
};
24+
25+
complex operator+(complex a, complex b) { return a+=b; }
26+
complex operator-(complex a, complex b) { return a-=b; }
27+
complex operator-(complex a) { return (-a.real(), -a.imag()); }
28+
bool operator==(complex a, complex b) {
29+
return a.real()==b.real() && a.imag()==b.imag();
30+
}
31+
bool operator!=(complex a, complex b) {
32+
return !(a==b);
33+
}
34+
complex sqrt(complex);
35+
36+
int main() {
37+
cout << "Complex Number Tests!" << endl;
38+
complex c(2, 3);
39+
cout << "Original: " << c.real() << "," << c.imag() << endl;
40+
complex c2(5, 6);
41+
complex c3 = c += c2;
42+
cout << "New" << c3.real() << "," << c3.imag() << endl;
43+
44+
return 0;
45+
}

c++/graph.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <utility>
4+
#include <tuple>
5+
6+
using namespace std;
7+
8+
int main() {
9+
vector<pair<int,int> > edges;
10+
edges.push_back(make_pair(1,2));
11+
edges.push_back(make_pair(2,5));
12+
for (auto e : edges) {
13+
cout << e.first << ", " << e.second << endl;
14+
}
15+
16+
vector<tuple<int,int,int> > weighted_edges;
17+
weighted_edges.push_back(make_tuple(1,2,3));
18+
weighted_edges.push_back(make_tuple(2,3,7));
19+
for (auto e : weighted_edges) {
20+
cout << get<0>(e) << ", " << get<1>(e) << ", " << get<2>(e) << endl;
21+
}
22+
return 0;
23+
}

c++/map.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
int main() {
8+
map<string, int> m;
9+
m["a"] = 1;
10+
m["b"] = 2;
11+
cout << "(a, " << m["a"] << ")" << endl;
12+
cout << "(b, " << m["b"] << ")" << endl;
13+
cout << "Size of map: " << m.size() << endl;
14+
// Check if element exists in map
15+
cout << "a exists: " << m.count("a") << endl;
16+
cout << "c doesn't exist: " << m.count("c") << endl;
17+
// Remove element in map
18+
m.erase("a");
19+
// Print everything in map
20+
for (auto x : m) {
21+
cout << "(" << x.first << "," << x.second << ")" << endl;
22+
}
23+
return 0;
24+
}

c++/matrix.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
template <typename T>
2+
class Matrix {
3+
public:
4+
void resize(int rows, int cols);
5+
T& at(int row, int col);
6+
int rows() { return row; }
7+
int cols() { return col; }
8+
private:
9+
int row;
10+
int col;
11+
T **data;
12+
};

c++/ref.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
class Person {
7+
public:
8+
Person(string name):name(name){};
9+
string name;
10+
};
11+
12+
void change_name(Person& p, string name) {
13+
p.name = name;
14+
}
15+
16+
int main() {
17+
Person a = Person("Alice");
18+
cout << a.name << endl;
19+
change_name(a, "Bob");
20+
cout << a.name << endl;
21+
return 0;
22+
}

c++/set.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <set>
3+
#include <string>
4+
#include <unordered_set>
5+
6+
using namespace std;
7+
8+
int main() {
9+
unordered_set<string> s;
10+
s.insert("a");
11+
s.insert("b");
12+
s.insert("c");
13+
// Size of set
14+
cout << "Size: " << s.size() << endl;
15+
// Check whether element exists
16+
cout << "Does a exist: " << s.count("a") << endl;
17+
cout << "Does d exist: " << s.count("d") << endl;
18+
// Remove a
19+
s.erase("a");
20+
cout << "New size: " << s.size() << endl;
21+
return 0;
22+
}

c++/template.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
template<class T, const int N>
2+
3+
class Things {
4+
T myArray[N];
5+
T getElement(int);
6+
void setElement(int, T);
7+
};
8+
9+
int main() {
10+
return 0;
11+
}

c++/vector.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <initializer_list>
2+
3+
using namespace std;
4+
5+
class Vector {
6+
private:
7+
double* elem;
8+
int sz;
9+
public:
10+
Vector(int s) : elem(new double[s]), sz(s) {
11+
for (int i = 0; i < s; i++) {
12+
elem[i] = 0;
13+
}
14+
}
15+
Vector(std::initializer_list<double> lst)
16+
: elem(new double[lst.size()]), sz(static_cast<int>(lst.size())) {
17+
copy(lst.begin(), lst.end(), elem);
18+
}
19+
~Vector() { delete[] elem; } // destructor
20+
double& operator[](int i);
21+
int size() const;
22+
void push_back(double);
23+
};
24+
25+
int main() {
26+
return 0;
27+
}

pb/cf/1579a.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
3+
#include <string>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
void run(string s) {
9+
size_t a = count(s.begin(), s.end(), 'A');
10+
size_t b = count(s.begin(), s.end(), 'B');
11+
size_t c = count(s.begin(), s.end(), 'C');
12+
if (a + c == b) {
13+
cout << "YES\n";
14+
} else {
15+
cout << "NO\n";
16+
}
17+
}
18+
19+
int main() {
20+
int n;
21+
cin >> n;
22+
while (n > 0) {
23+
string s;
24+
cin >> s;
25+
run(s);
26+
n--;
27+
}
28+
return 0;
29+
}

0 commit comments

Comments
 (0)