0% found this document useful (0 votes)
11 views2 pages

Buoi 4

The document contains 5 code snippets that implement different algorithms: 1. A recursive function to sum all even or odd numbers between a range. 2. A placeholder that the author has not implemented the algorithm yet. 3. A recursive function to calculate the multiplication of two numbers as a sum of additions. 4. A recursive function to check if a string is a palindrome by comparing first and last characters at each step. 5. A recursive function to solve the Tower of Hanoi problem by moving disks one by one from the source to destination pole.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Buoi 4

The document contains 5 code snippets that implement different algorithms: 1. A recursive function to sum all even or odd numbers between a range. 2. A placeholder that the author has not implemented the algorithm yet. 3. A recursive function to calculate the multiplication of two numbers as a sum of additions. 4. A recursive function to check if a string is a palindrome by comparing first and last characters at each step. 5. A recursive function to solve the Tower of Hanoi problem by moving disks one by one from the source to destination pole.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Bai 1:

#include <iostream>
using namespace std;
int sum(int begin, int end,int &s){
if (begin>end)
return s;
else {
s+=begin;
return sum(begin+2,end,s) ;
}
}
int main(){
int begin, end, s=0;
cin>>begin;
cin>>end;
if (begin%2==0){
cout<<"Tong chan: "<<sum(begin,end,s)<<endl; s=0;
cout<<"Tong le: "<<sum(begin+1,end,s); s=0;
}
else {
cout<<"Tong chan: "<<sum(begin+1,end,s)<<endl; s=0;
cout<<"Tong le: "<<sum(begin,end,s); s=0;
}
return 0;
}
******************************************************

Bai 2: Em chua biet lam !

******************************************************

Bai 3:
#include <iostream>
#include <math.h>
using namespace std;
int Tich(int a, int b){
if (a>b){
if (b==1) return a;
else return a+Tich(a,b-1);
}
else {
if (a==1) return b;
else return b+Tich(a-1,b);
}
}
int main(){
int a,b, k;
cin>>a>>b;
if ((a<0&&b>0 )|| (a>0&&b<0)){
cout<<"-"<<Tich(abs(a),abs(b))<<endl;
}
else
cout<<Tich(a,b)<<endl;
return 0;
}
******************************************************
Bai 4:
#include <iostream>
#include <string>
using namespace std;
bool Check(string s, int &i){
if (i>=s.size()/2) return true;
else {
if (s[i]!=s[s.size()-i-1]) return false;
else {
i+=1;
return Check(s,i);
}
}
}
int main(){
string P;
int i=0 ;
cin>>P;
if(Check(P, i)) cout<<"TRUE"<<endl;
else cout<<"FALSE"<<endl;
return 0;
}
******************************************************
Bai 5:

#include <iostream>
using namespace std;
void move(int n, int x, int y){
if (n==1) {
cout<<x<<"->"<<y<<", ";
}
else {
move(n-1, x, 6-y-x);
move(1, x, y);
move(n-1, 6-x-y, y);
}
}
int main(){
int n, x, y;
cout<<"so dia: "; cin>>n;
cout<<"coc nguon: "; cin>>x;
cout<<"coc dich: "; cin>>y;
move(n, x, 6-x-y);
return 0;
}

You might also like