Browse Source

Add some stuff from cses.fi

master
Abheek Dhawan 3 years ago
parent
commit
eef132e10b
  1. 42
      cses/Create Strings.cpp
  2. 30
      cses/Distinct Numbers.cpp
  3. 19
      cses/Increasing Array/index.cpp
  4. 21
      cses/Missing Number/index.cpp
  5. 32
      cses/Number Spiral/index.cpp
  6. 14
      cses/Palindrome Reorder.cpp
  7. 29
      cses/Permutations/index.cpp
  8. 22
      cses/Repetitions/index.cpp
  9. 14
      cses/Trailing Zeros.cpp
  10. 20
      cses/Trailing Zeros/index.cpp
  11. 18
      cses/Weird Algorithm/index.cpp

42
cses/Create Strings.cpp

@ -0,0 +1,42 @@
#include <algorithm>
#include <iostream>
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()));
}

30
cses/Distinct Numbers.cpp

@ -0,0 +1,30 @@
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> 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;
}

19
cses/Increasing Array/index.cpp

@ -0,0 +1,19 @@
#include <iostream>
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;
}

21
cses/Missing Number/index.cpp

@ -0,0 +1,21 @@
#include <iostream>
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;
}

32
cses/Number Spiral/index.cpp

@ -0,0 +1,32 @@
#include <iostream>
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;
}
}

14
cses/Palindrome Reorder.cpp

@ -0,0 +1,14 @@
#include <iostream>
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 << " ";
}

29
cses/Permutations/index.cpp

@ -0,0 +1,29 @@
#include <iostream>
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 << " ";
}
}
}

22
cses/Repetitions/index.cpp

@ -0,0 +1,22 @@
#include <iostream>
#include <vector>
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;
}

14
cses/Trailing Zeros.cpp

@ -0,0 +1,14 @@
#include <cmath>
#include <iostream>
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;
}

20
cses/Trailing Zeros/index.cpp

@ -0,0 +1,20 @@
#include <cmath>
#include <iostream>
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;
}

18
cses/Weird Algorithm/index.cpp

@ -0,0 +1,18 @@
#include <iostream>
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 << " ";
}
}
}
Loading…
Cancel
Save