Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- void solve_rec_h(int V[], int T[], int length, int i, int even, int odd);
- void solve_rec(int V[], int T[], int length);
- int max_occurrence_re(int V[], int length);
- int max_occurrence_rec_h(int V[], int length, int i, int j, int max_occ, int count);
- int count_couples_h(int V[], int length, int i, int j, int max_occ, int count);
- int count_character(char V[], char c, int size, int i);
- int count_couples(char V[], int size);
- int count_couples_h(char V[], int size, int i);
- void solve_rec(int V[], int T[], int length) {
- return solve_rec_h(V, T, length, 0, 0, length - 1);
- }
- void solve_rec_h(int V[], int T[], int length, int i, int even, int odd) {
- if (i == length) return;
- if (V[i] % 2 == 0) {
- T[even] = V[i];
- even++;
- } else {
- T[odd] = V[i];
- odd--;
- }
- return solve_rec_h(V, T, length, i + 1, even, odd);
- }
- int max_occurrence(int V[], int length) {
- int max_occ = 0;
- for (int i = 0; i < length; i++) {
- int count = 0;
- for (int j = i; j < length; j++) {
- if (V[i] == V[j]) {
- count++;
- }
- }
- if (count > max_occ) {
- max_occ = count;
- }
- }
- return max_occ;
- }
- int max_occurrence_rec(int V[], int length) {
- return max_occurrence_rec_h(V, length, 0, 0, 0, 0);
- }
- int max_occurrence_rec_h(int V[], int length, int i, int j, int max_occ,
- int count) {
- std::cout << i << " " << j << std::endl;
- if (i == length) return max_occ;
- if (j == length) {
- if (count > max_occ) max_occ = count;
- return max_occurrence_rec_h(V, length, i + 1, i + 1, max_occ, 0);
- }
- if (V[i] == V[j]) {
- count++;
- }
- return max_occurrence_rec_h(V, length, i, j + 1, max_occ, count);
- }
- void solve(int V[], int T[], int length) {
- int even = 0, odd = length - 1;
- for (int i = 0; i < length; i++) {
- // For each item in V
- if (V[i] % 2 == 0) {
- T[even] = V[i];
- even++;
- } else {
- T[odd] = V[i];
- odd--;
- }
- }
- }
- int count_character(char V[], char c, int size, int i) {
- if (i == size) return 0;
- if (V[i] == c) {
- return count_character(V, c, size, i + 1) + 1;
- }
- return count_character(V, c, size, i + 1);
- }
- int count_couples(char V[], int size) {
- return count_couples_h(
- V, size, 0
- ) / 2;
- }
- int count_couples_h(char V[], int size, int i) {
- if (i == size) return 0;
- int result = count_character(V, V[i], size, 0);
- if (result == 2) {
- return count_couples_h(V, size, i + 1) + 1;
- }
- return count_couples_h(V, size, i + 1);
- }
- int main() {
- int ARR[] = {1, 4, 2, 4, 7, 2, 2, 2, 0};
- int SORTED[9];
- char T[] = "tonno_papera";
- std::cout << count_couples(T, strlen(T));
- // solve_rec(ARR, SORTED, 9);
- // std::cout << "Max occ: " << max_occurrence_rec(ARR, 9);
- // for (int i = 0; i < 9; i++) {
- // std::cout << SORTED[i] << " ";
- // }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement