0% found this document useful (0 votes)
5 views

Recursion

Uploaded by

Hello Duy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Recursion

Uploaded by

Hello Duy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

using namespace std;

// 2)

void printArray(int n, int current = 0)


{
if (current == n)
{
cout << n;
}
else
{
cout << current << ", ";
printArray(n, current + 1);
}
}

// 3)

void printPattern(int n)
{
if (n <= 0)
{
cout << n;
return;
}

cout << n << " ";


printPattern(n - 5);
cout << " " << n;
}

// 4)

int findMax(int* arr, int length)


{
if (length == 1)
{
return *arr;
}

int max = findMax(arr + 1, length - 1);

if (*arr > max)


{
return *arr;
}

return max;
}

// 5)

bool isPalindrome(const string& str)


{
if (str.length() <= 1)
{
return true;
}

char first = tolower(str[0]);


char last = tolower(str[str.length() - 1]);

if (!isalnum(first))
{
return isPalindrome(str.substr(1));
}
else if (!isalnum(last))
{
return isPalindrome(str.substr(0, str.length() - 1));
}
else if (first != last)
{
return false;
}

return isPalindrome(str.substr(1, str.length() - 2));


}

// 6)

int findGCD(int a, int b)


{
if(b == 0) return a;
return findGCD(b, a%b);
}

// 7)
string repeatString(string str, int n) {
if (n <= 0) {
return "";
}

return str + repeatString(str, n - 1);


}

string expand(string s, size_t& index) {


if (index >= s.length() || s[index] == ')') {
return "";
}
string result;

if (isdigit(s[index])) {
int n = s[index] - '0';
index += 2;
string subString = expand(s, index);
result += repeatString(subString, n);
}
else {
result += s[index];
}
result += expand(s, ++index);
return result;
}

string expand(string s) {
size_t index = 0;
return expand(s, index);
}

// 8)

void printHailstone(int number)


{
cout<<number;
if(number == 1) return;
if(number % 2 == 0)
{
cout<<" ";
printHailstone(number/2);
}
else
{
cout<<" ";
printHailstone(number*3+1);
}
}

// 9)
#include <cmath>
int myArrayToInt(char* str, int n)
{
if (n == 1)
{
return str[0] - '0';
}

int lastDigit = str[n - 1] - '0';


int other_nums = myArrayToInt(str, n - 1);

return other_nums * 10 + lastDigit;


}

// 10)

int findGCD(int a, int b)


{
if (b == 0)
{
return a;
}

return findGCD(b, a % b);


}

int findLCM(int a, int b)


{
return (a/findGCD(a,b))*b;
}

// 11)
int mininumBracketAdd(const string& s, size_t index = 0, int openBrackets = 0) {
if (index >= s.length()) {
return openBrackets;
}

int count = 0;

if (s[index] == '(') {
openBrackets++;
} else if (s[index] == ')' && openBrackets > 0) {
openBrackets--;
} else if (s[index] == ')') {
count++;
}

return count + mininumBracketAdd(s, index + 1, openBrackets);


}

//12)
string reverseSentence(string s) {
if(s.empty())
{
return "";
}

size_t spaceIndex = s.find(' ');

if (spaceIndex == string::npos)
{
return s;
}

string firstWord = s.substr(0, spaceIndex);


string remainingSentence = s.substr(spaceIndex + 1);

string reversedRemaining = reverseSentence(remainingSentence);

if (reversedRemaining.empty())
{
return firstWord;
}
else
{
return reversedRemaining + ' ' + firstWord;
}
}

//13)
int strLen(char* str)
{
if(*str == '\0') return 0;
else
{
return 1 + strLen(str+1);
}
}

You might also like