Buoi 4
Buoi 4
#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 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;
}