C++ Algorithm and Data Structure Examples
Posted on Nov 16, 2024 in Computers
Greatest Common Divisor (GCD)
C++ Code:
#include <iostream>
using namespace std;
int gcd(int a, int b) {
int aux;
while (b > 0) {
aux = b;
b = a % b;
a = aux;
}
return a;
}
Base Conversion
C++ Code:
#include <iostream>
using namespace std;
void base2(int n) {
if (n <= 1) cout << n;
else {
base2(n / 2);
cout << n % 2;
}
}
void base8(int n) {
if (n / 8 == 0) cout << n;
else {
base8(n / 8);
cout << n % 8;
}
}
void base16(int n) {
if (n / 16 == 0) {
if (n == 10) {
cout << 'A';
} else if (n == 11) {
cout << 'B';
} else if (n == 12) {
cout << 'C';
} else if (n == 13) {
cout << 'D';
} else if (n == 14) {
cout << 'E';
} else if (n == 15) {
cout << 'F';
} else {
cout << n;
}
} else {
base16(n / 16);
int mod;
mod = n % 16;
if (mod == 10) {
cout << 'A';
} else if (mod == 11) {
cout << 'B';
} else if (mod == 12) {
cout << 'C';
} else if (mod == 13) {
cout << 'D';
} else if (mod == 14) {
cout << 'E';
} else if (mod == 15) {
cout << 'F';
} else {
cout << mod;
}
}
}
int main() {
int n;
while (cin >> n) {
cout << n << " = ";
base2(n);
cout << ", ";
base8(n);
cout << ", ";
base16(n);
cout << endl;
}
}
Consecutive Repeated Words
C++ Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
int max = 1, act = 1;
string pact, sbusc;
cin >> sbusc;
while (cin >> pact) {
if (sbusc == pact) {
act++;
if (act > max) {
max = act;
}
} else {
act = 0; // Reset count if words are different
}
}
cout << max << endl;
}
Primality Test
C++ Code:
#include <iostream>
#include <cmath>
using namespace std;
bool esprimer(int n) {
int i = 3;
bool p = true;
int max;
max = sqrt(n);
if (n == 2) p = true;
else if (n == 1) p = false;
else if (n % 2 == 0) p = false;
else {
while (i <= max and p) {
if (n % i == 0) p = false;
i = i + 2;
}
}
return p;
}
int main() {
int n, p;
cin >> n;
while (n > 0) {
cin >> p;
cout << p;
if (not esprimer(p)) cout << " no";
cout << " es primer" << endl;
n--;
}
}
Balanced Numbers
C++ Code:
#include <iostream>
using namespace std;
bool es_equilibrat(int n) {
int numimp = 0, numpar = 0;
while (n > 0) {
numimp += n % 10;
n /= 10;
numpar += n % 10;
n /= 10;
}
if (numimp == numpar) return true;
return false;
}
int main() {
int n;
cin >> n;
if (es_equilibrat(n)) cout << "si" << endl;
else cout << "no" << endl;
}
Valid Dates
C++ Code:
#include <iostream>
using namespace std;
bool es_any_de_traspas(int any) {
if ((any % 4 == 0 && any % 100 != 0) || any % 400 == 0) {
return true;
} else {
return false;
}
}
bool es_data_valida(int d, int m, int a) {
if (d > 0 && d < 32 && m > 0 && m < 13) {
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { // 31 days
if (d < 32) {
return true;
} else {
return false;
}
} else if (m == 2) { // February
if (es_any_de_traspas(a)) {
if (d < 30) {
return true;
} else {
return false;
}
} else {
if (d < 29) {
return true;
} else {
return false;
}
}
} else { // 30 days
if (d < 31) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}
int main() {
int d, m, a;
cin >> d >> m >> a;
cout << es_data_valida(d, m, a) << endl;
}
Least Common Multiple (LCM)
C++ Code:
#include <iostream>
using namespace std;
int gcd(int a, int b) {
int aux;
while (b > 0) {
aux = b;
b = a % b;
a = aux;
}
return a;
}
int lcm(int a, int b) {
return ((a) / gcd(a, b)) * b;
}
int main() {
int n;
int a, b;
int mincum;
while (cin >> n and n != 0) {
if (n == 1) {
cin >> a;
cout << a << endl;
} else {
cin >> a;
for (int i = 0; i < n - 1; ++i) {
cin >> b;
mincum = lcm(a, b);
a = mincum;
}
cout << mincum << endl;
}
}
}
Digit Reduction
C++ Code:
#include <iostream>
using namespace std;
int suma_digits(int x) {
if (x / 10 == 0) {
return x;
} else {
return x % 10 + suma_digits(x / 10);
}
}
int reduccio_digits(int x) {
if (x < 10) {
return x;
}
return reduccio_digits(suma_digits(x));
}
int main() {
int n;
cin >> n;
cout << reduccio_digits(n) << endl;
}
Reversing a List of Words
C++ Code:
#include <iostream>
#include <string>
using namespace std;
int gira_paraules(int n) {
string mot;
if (not(cin >> mot)) {
return 0;
}
int d = gira_paraules(n);
if (d < n) {
cout << mot << endl;
}
return d + 1;
}
int main() {
int n;
cin >> n;
gira_paraules(n);
}
Maximum of Each Sequence
C++ Code:
#include <iostream>
using namespace std;
int main() {
int n, act, max, i = 0;
while (cin >> n) {
cin >> max;
while (i < n - 1) {
cin >> act;
if (act >= max) {
max = act;
}
i++;
}
cout << max << endl;
i = 0; // Reset i for the next sequence
}
}
Caesar Cipher
C++ Code:
#include <iostream>
using namespace std;
int main() {
int n, ic;
char c;
while (cin >> n) {
while (cin >> c && c != '.') {
if ((int)c >= 97 && (int)c <= 122) {
ic = (int)c;
ic += n;
while (ic > 122) {
ic = ic - 26;
}
ic = ic - 32; // Convert to uppercase
cout << (char)ic;
} else if (c == '_') {
cout << ' ';
} else {
cout << c;
}
}
cout << endl;
}
}