I forgot to make commits for each problem so here's a bunch in one go.

This commit is contained in:
Jake Bauer 2023-10-20 22:06:45 +02:00
parent f738e0b869
commit 7917bc182f
11 changed files with 302 additions and 1 deletions

1
.gitignore vendored
View File

@ -31,4 +31,3 @@
*.exe
*.out
*.app

22
bit-strings.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <iostream>
int
main(void)
{
int n;
std::cin >> n;
long x = 1;
long b = 2;
while (n != 0)
{
if (n % 2 == 1)
{
x = x * b % 1000000007;
}
b = b * b % 1000000007;
n >>= 1;
}
std::cout << x << std::endl;
}

39
increasing-array.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
int
main(void)
{
// oops, I left my debug printing in my first submission :<
size_t n;
std::cin >> n;
int *a = (int *)calloc(n, sizeof(int));
std::string input;
getline(std::cin, input); // discard extra newline
getline(std::cin, input);
std::istringstream iss(input);
std::string s;
for (size_t i = 0; getline(iss, s, ' '); i++)
{
a[i] = stoi(s);
}
long moves = 0;
int diff = 0;
for (size_t i = 1; i < n; i++)
{
if (a[i] > a[i-1])
{
continue;
}
diff = a[i-1] - a[i];
a[i] += diff;
moves += diff;
}
std::cout << moves;
}

View File

@ -0,0 +1,33 @@
#include <iostream>
#include <string>
int
main(void)
{
std::string input;
getline(std::cin, input);
long longest_length = 1;
long current_length = 1;
char new_letter, current_letter = '0';
for (size_t i = 0; i < input.length(); i++)
{
new_letter = input[i];
if (new_letter != current_letter)
{
if (current_length > longest_length)
{
longest_length = current_length;
}
current_length = 0;
current_letter = new_letter;
}
current_length++;
}
if (current_length > longest_length)
{
longest_length = current_length;
}
std::cout << longest_length;
}

33
missing-number.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
int
main(void)
{
size_t n;
std::cin >> n;
bool *a = (bool *)calloc(n, sizeof(bool));
std::string input;
getline(std::cin, input); // discard extra newline
getline(std::cin, input);
std::istringstream iss(input);
std::string s;
while (getline(iss, s, ' '))
{
a[stoi(s)-1] = 1;
}
for (size_t i = 0; i < n; i++)
{
if (!a[i])
{
std::cout << i+1;
break;
}
}
}

37
number-spiral.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
int
main(void)
{
long t;
std::cin >> t;
long x, y, c;
for (long i = 0; i < t; i++)
{
std::cin >> y >> x;
if (x > y)
{
if (x % 2 == 0)
{
c = (x * x) - (x + x) + y + 1;
}
else
{
c = (x * x) - y + 1;
}
}
else
{
if (y % 2 == 0)
{
c = (y * y) - x + 1;
}
else
{
c = (y * y) - (y + y) + x + 1;
}
}
std::cout << c << std::endl;
}
}

30
permutations.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include <string>
int
main(void)
{
long n;
long x;
std::cin >> n;
std::string solution = "";
if (n == 2 || n == 3)
{
std::cout << "NO SOLUTION";
return 0;
}
for (x = 2; x <= n; x += 2)
{
solution += std::to_string(x) + " ";
}
for (x = 1; x <= n; x += 2)
{
solution += std::to_string(x) + " ";
}
std::cout << solution;
}

29
trailing-zeroes.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
int
main(void)
{
int n;
std::cin >> n;
if (n < 0)
{
std::cout << -1 << std::endl;
return 1;
}
int x = 0;
// Observed pattern where the number of trailing zeroes increases by
// one for every additional factor of 5.
// e.g. 0! to 4! has 0, 5! to 9! has 1, 10! to 14! has 2, and so on.
// Turns out you need to consider additional powers of 5 (25, 125, etc).
// This was not obvious at first since 25! overflowed.
for (int i = 5; n / i >= 1; i *= 5)
{
x += n / i;
}
std::cout << x << std::endl;
return 0;
}

13
two-knights.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
int
main(void)
{
long n;
std::cin >> n;
for (long k = 1; k <= n; k++)
{
std::cout << ((k * k) * ((k * k) - 1)) / 2 - 4 * (k - 1) * (k - 2) << std::endl;
}
}

43
two-sets.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <iostream>
#include <vector>
int
main(void)
{
long n;
std::cin >> n;
std::vector<long> A, B;
// If sum of {1, 2, ..., n} is odd, no way to make 2 sets with equal sum
long sum = (n * (n + 1)) / 2;
if (sum % 2 == 1)
{
std::cout << "NO";
return 0;
}
std::cout << "YES" << std::endl;
// Break sum in half; this is what each set will add up to
sum /= 2;
for (long i = n; i > 0; i--)
{
if (i <= sum)
{
A.push_back(i);
sum -= i;
}
else
{
B.push_back(i);
}
}
std::cout << A.size() << std::endl;
for (long x : A)
std::cout << x << " ";
std::cout << std::endl << B.size() << std::endl;
for (long x : B)
std::cout << x << " ";
}

23
weird-algorithm.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <iostream>
using namespace std;
int
main(void)
{
long x;
cin >> x;
while (x != 1)
{
cout << x << " ";
if (x % 2 == 0)
{
x /= 2;
}
else
{
x = (x * 3) + 1;
}
}
cout << x;
}