From eef132e10bef52c594cf8a5a7ec4efcd90be91ad Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Wed, 17 Nov 2021 11:24:47 -0600 Subject: [PATCH] Add some stuff from cses.fi --- cses/Create Strings.cpp | 42 +++++++++++++++++++++++++++++++++ cses/Distinct Numbers.cpp | 30 +++++++++++++++++++++++ cses/Increasing Array/index.cpp | 19 +++++++++++++++ cses/Missing Number/index.cpp | 21 +++++++++++++++++ cses/Number Spiral/index.cpp | 32 +++++++++++++++++++++++++ cses/Palindrome Reorder.cpp | 14 +++++++++++ cses/Permutations/index.cpp | 29 +++++++++++++++++++++++ cses/Repetitions/index.cpp | 22 +++++++++++++++++ cses/Trailing Zeros.cpp | 14 +++++++++++ cses/Trailing Zeros/index.cpp | 20 ++++++++++++++++ cses/Weird Algorithm/index.cpp | 18 ++++++++++++++ 11 files changed, 261 insertions(+) create mode 100644 cses/Create Strings.cpp create mode 100644 cses/Distinct Numbers.cpp create mode 100644 cses/Increasing Array/index.cpp create mode 100644 cses/Missing Number/index.cpp create mode 100644 cses/Number Spiral/index.cpp create mode 100644 cses/Palindrome Reorder.cpp create mode 100644 cses/Permutations/index.cpp create mode 100644 cses/Repetitions/index.cpp create mode 100644 cses/Trailing Zeros.cpp create mode 100644 cses/Trailing Zeros/index.cpp create mode 100644 cses/Weird Algorithm/index.cpp diff --git a/cses/Create Strings.cpp b/cses/Create Strings.cpp new file mode 100644 index 0000000..b9eaea9 --- /dev/null +++ b/cses/Create Strings.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +long find_fact(int num) { + long num_perms = 1; + for (int i = 0; i < num; i++) { + num_perms *= num - i; + } + return num_perms; +} + +int main() { + string m; + cin >> m; + string n = m; + sort(n.begin(), n.end()); + + long num_perms = 1; + int current_repetition = 1; + for (int i = 0; i < n.length(); i++) { + num_perms *= n.length() - i; + if (i > 0) { + if (n.at(i) == n.at(i - 1)) { + current_repetition += 1; + } else { + if (current_repetition > 1) { + num_perms /= find_fact(current_repetition); + } + current_repetition = 1; + } + } + } + if (current_repetition > 1) { + num_perms /= find_fact(current_repetition); + } + + cout << num_perms << endl; + do { + cout << n << endl; + } while (next_permutation(n.begin(), n.end())); +} diff --git a/cses/Distinct Numbers.cpp b/cses/Distinct Numbers.cpp new file mode 100644 index 0000000..70bb142 --- /dev/null +++ b/cses/Distinct Numbers.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +using namespace std; + +int main() { + int n; + cin >> n; + vector x; + + int a = 1; + + for (int i = 0; i < n; i++) { + int j; + cin >> j; + x.push_back(j); + } + + sort(x.begin(), x.end()); + + for (unsigned int i = 0; i < x.size() - 1; i++) { + // cout << x.at(i) << " "; + if (x.at(i) < x.at(i + 1)) { + a++; + } + } + cout << a; +} diff --git a/cses/Increasing Array/index.cpp b/cses/Increasing Array/index.cpp new file mode 100644 index 0000000..ac73fa8 --- /dev/null +++ b/cses/Increasing Array/index.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main() { + int n; + cin >> n; + int px = 0; + long long ans = 0; + for (int i = 0; i < n; i++) { + int x; + cin >> x; + if (x < px) { + ans += px - x; + x = px; + } + px = x; + } + cout << ans; +} diff --git a/cses/Missing Number/index.cpp b/cses/Missing Number/index.cpp new file mode 100644 index 0000000..8545dce --- /dev/null +++ b/cses/Missing Number/index.cpp @@ -0,0 +1,21 @@ +#include +using namespace std; +long findMissingNo(long arr[], long len) { + long temp; + temp = ((len) * (len + 1)) / 2; + for (long i = 0; i < len - 1; i++) + temp -= arr[i]; + return temp; +} + +int main() { + long n; + cin >> n; + long arr[n]; + for (long i = 0; i < n - 1; i++) { + cin >> arr[i]; + } + long missingNo = findMissingNo(arr, n); + cout << missingNo; + return 0; +} diff --git a/cses/Number Spiral/index.cpp b/cses/Number Spiral/index.cpp new file mode 100644 index 0000000..2bf86ef --- /dev/null +++ b/cses/Number Spiral/index.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +int main() { + long long n; + cin >> n; + long long y, x; + long long v; + for (long long i = 0; i < n; i++) { + cin >> y; + cin >> x; + + if (y > x) { + if (y % 2 == 0) { + v = y * y; + v -= x - 1; + } else { + v = (y - 1) * (y - 1) + 1; + v += x - 1; + } + } else { + if (x % 2 == 0) { + v = (x - 1) * (x - 1) + 1; + v += y - 1; + } else { + v = x * x; + v -= y - 1; + } + } + cout << v << endl; + } +} diff --git a/cses/Palindrome Reorder.cpp b/cses/Palindrome Reorder.cpp new file mode 100644 index 0000000..0a601f1 --- /dev/null +++ b/cses/Palindrome Reorder.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main() { + bool solution; + string x; + cin >> x; + int c[26]; + for (int i = 0; i < x.length(); i++) { + c[(int)x.at(i) - 48]++; + } + for (int i : c) + cout << i << " "; +} diff --git a/cses/Permutations/index.cpp b/cses/Permutations/index.cpp new file mode 100644 index 0000000..7efb3d7 --- /dev/null +++ b/cses/Permutations/index.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main() { + int n; + cin >> n; + + if (n == 1) { + cout << 1; + return 0; + } + + if (n == 2 || n == 3) { + cout << "NO SOLUTION"; + return 0; + } + + for (long long i = 1; i <= n; i++) { + if (i % 2 == 0) { + cout << i << " "; + } + } + + for (long long i = 1; i <= n; i++) { + if (i % 2 == 1) { + cout << i << " "; + } + } +} diff --git a/cses/Repetitions/index.cpp b/cses/Repetitions/index.cpp new file mode 100644 index 0000000..15fb99f --- /dev/null +++ b/cses/Repetitions/index.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + +int main() { + string s; + cin >> s; + int ans = 0, c = 0; + + char l = 'A'; + for (char d : s) { + if (d == l) { + ++c; + ans = max(c, ans); + } else { + l = d; + c = 1; + } + } + + cout << ans; +} diff --git a/cses/Trailing Zeros.cpp b/cses/Trailing Zeros.cpp new file mode 100644 index 0000000..16cb243 --- /dev/null +++ b/cses/Trailing Zeros.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int main() { + long n; + cin >> n; + + int a = 0; + for (int i = 0; i < 100; i++) { + a += floor(n / pow(5, i + 1)); + } + cout << a; +} diff --git a/cses/Trailing Zeros/index.cpp b/cses/Trailing Zeros/index.cpp new file mode 100644 index 0000000..e9165f8 --- /dev/null +++ b/cses/Trailing Zeros/index.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +int main() { + long n; + cin >> n; + + int a; + /* a += floor(n / 5); + a += floor(n / 25); + a += floor(n / 125); + a += floor(n / 625); + a += floor(n / 3125); + a += floor(n / 15625);*/ + for (int i = 0; i < 100; i++) { + a += floor(n / pow(5, i + 1)); + } + cout << a; +} diff --git a/cses/Weird Algorithm/index.cpp b/cses/Weird Algorithm/index.cpp new file mode 100644 index 0000000..745c64d --- /dev/null +++ b/cses/Weird Algorithm/index.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main() { + long long i; + cin >> i; + cout << i << " "; + + while (i != 1) { + if (i % 2 == 0) { + i = i / 2; + cout << i << " "; + } else { + i = i * 3 + 1; + cout << i << " "; + } + } +}