Professional Documents
Culture Documents
(Lib4u.net) Dap An Giao Trinh 77 Bai Tap C++
(Lib4u.net) Dap An Giao Trinh 77 Bai Tap C++
getch();
return 0;
}
2
+Ch : rt ngn bi gii v khi phi khai bo thm bin x1, x2. Ta c th dng
hm cout a ra mn hnh gi tr ca x1 hay x2 thng qua biu thc tnh ca n v t biu thc
trong float() a ra mn hnh gi tr thc
Bi 11:
#include <iostream> using namespace std;
#include <conio>
int main() {
float a,b,c,x;
cout<<"Nhap a,b,c,x ";
cin>>a>>b>>c>>x;
cout<<"f= "<<float(a*x*x+b*x+c);
getch();
return 0;
}
Bi 13:
#include<iostream>
using namespace std;
void main()
{
int a;
cout<<"Nhap vao mot so nguyen ";
cin>>a;
if (a%2==0) cout<<"So chan";
else cout<<"So le";
cout<<"\n";
}
Bi 15:
#include <iostream> using namespace std;
#include <conio>
int main() {
char a,b; //B1
int c,d;
cout<<"Anh ra cai \n"; //B2
cin>>a;
if (a=='B') c=1;
3
01
-------------10 ------> s 2 trong h thp phn
Nu 2 s ging nhau th tt nhin tr v 00 tc l s 0. H h v cng hng hiu ti sao trong
phm vi 1 n 3 th c 2 s khc nhau th tr v s cn li. Khng bit ti y cn ai cha hiu
na nh
B4: Bc cn li th n gin, n tr v s no th ta bit k t tng ng vi s Anh v Em
u khng on ra, ta dng If gii quyt ngon phn cn li .
Bi 16:
#include <iostream> using namespace std;
#include <conio>
int main() {
const int luongcb=650;
int tnct;
double heso,luong;
cout<<" Cho biet tham nien cong tac \n";
cin>>tnct;
if (tnct < 12) heso = 1.92;
else if (tnct < 36) heso = 2.34;
else if (tnct < 60) heso = 3;
else heso = 4.5;
luong=luongcb*heso;
cout<<"Luong = "<<luong<<"000 ngan dong";
getch();
return 0;
}
Bi 17:
#include <iostream> using namespace std;
#include <conio>
int main() {
int giobd,giokt,thoigian,tien;
do {
cout<<"Nhap gio bat dau \n";
cin>>giobd;
cout<<"Nhap gio ket thuc \n";
cin>>giokt;
6
return 0;
}
Bi 19:
#include <iostream> using namespace std;
#include <conio>
int main() {
int a[3],i,j,t;
cout<<"Nhap 3 so \n";
cin>>a[0]>>a[1]>>a[2];
for(i=0;i<2;i++)
for(j=i+1;j<3;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
cout<<"Max= "<<a[2]<<endl;
cout<<"Min= "<<a[0];
getch();
return 0;
}
Bi 20:
#include <iostream> using namespace std;
#include <conio>
int main() {
int a[4],i,j,t;
cout<<"Nhap 4 so \n";
cin>>a[0]>>a[1]>>a[2]>>a[3];
for(i=0;i<3;i++)
for(j=i+1;j<4;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
cout<<"Max= "<<a[3]<<endl;
8
cout<<"Min= "<<a[0];
getch();
return 0;
}
Bi 22:
#include <iostream> using namespace std;
#include <conio>
int main() {
double a,b;
do {
cout<<"Nhap 2 so \n";
cin>>a>>b;
} while (a==0);
if (a>0) cout<<"Nghiem: x > "<<-b/a;
else cout<<"Nghiem: x < "<<-b/a;
getch();
return 0;
}
Bi 23:
#include <conio>
#include <math>
int main() {
double a,b,c,d;
do {
cout<<"Nhap tu va mau phan so 1 \n";
cin>>a>>b;
} while (b==0);
do {
cout<<"Nhap tu va mau phan so 2 \n";
cin>>c>>d;
} while (d==0);
cout<<"Tong = "<<(a/b+c/d)<<endl;
cout<<"Hieu = "<<abs(a/b-c/d)<<endl;
cout<<"Tich = "<<(a/b*c/d)<<endl;
if (c!=0) cout<<"Thuong = "<<((a/b)/(c/d));
else cout<<"Thuong khong tinh duoc.";
getch();
return 0;
}
Bi 24:
#include <iostream> using namespace std;
#include <conio>
int main() {
char a;
int b;
cout<<"Nhap ki tu \n";
cin>>a;
b=int(a);
if ((b>=48)&&(b<=57)) cout<<"La ki tu so";
else if ((b>=65)&&(b<=90)) cout<<"La ki tu chu in hoa";
else if ((b>=97)&&(b<=122))
cout<<"La ki tu chu thuong";
else cout<<"La ki tu dac biet";
getch();
return 0;
}
Bi 25:
#include <iostream> using namespace std;
#include <conio>
int main() {
unsigned int a;
long st;
cout<<"Nhap luong dien tieu thu (kWh) \n";
cin>>a;
if (a<=100) st=a*550;
else if ((a>100)&&(a<=150)) st=100*550+(a-100)*900;
else if ((a>150)&&(a<=200)) st=100*550+50*900+(a-150)*1250;
else if ((a>200)&&(a<=300))
st=100*550+50*900+50*1250+(a-200)*1450;
else st=100*550+50*900+50*1250+100*1450+(a-300)*1700;
cout<<"So tien phai tra la "<<st*1.1<<" VND.";
getch();
return 0;
}
10
Bi 26:
#include <iostream> using namespace std;
#include <conio>
int main() {
unsigned int MS;
int X,Y,t;
do {
cout<<"Nhap ma so nhan vien \n";
cin>>MS;
} while (MS>4);
if (MS==1) {
cout<<"Nhap khoan luong co dinh X \n";
cin>>X;
cout<<"Luong cho nha quan li la "<<X<<" VND.";
}
else if (MS==2) {
cout<<"Nhap khoan luong co ban Y \n";
cin>>Y;
cout<<"Nhap so h lam duoc trong tuan \n";
cin>>t;
if (t<=40) X=40*Y;
else X=(t-40)*1.5*Y+40*Y;
cout<<"Luong cho cong nhan lam theo gio la "<<X<<" VND.";
}
else if (MS==3) {
cout<<"Nhap tri gia doanh so Z \n";
cin>>Y;
cout<<"Luong cong nhan theo loi nhuan la "<<(500000+0.7*Y)<<" VND.";
}
else if (MS==4) {
cout<<"Nhap so N san pham lam ra trong tuan \n";
cin>>X;
cout<<"Nhap so tien S tra cho moi san pham \n";
cin>>Y;
cout<<"Luong cong nhan theo san pham la "<<X*Y<<" VND.";
}
getch();
return 0;
}
11
Bi 27:
#include <iostream> using namespace std;
#include <conio>
#include <stdlib>
const char* chu[10] = {"khong", "mot", "hai", "ba", "bon", "nam", "sau", "bay", "tam", "chin"};
int main() {
int a,hangchuc,hangdonvi;
cout<<"Nhap so nguyen 2 chu so \n";
cin>>a;
hangchuc = a/10;
hangdonvi = a - hangchuc*10;
if (a==10) cout<<"muoi";
else if (a<20) cout<<"muoi "<<chu[hangdonvi];
else cout<<chu[hangchuc]<<" muoi "<<chu[hangdonvi];
getch();
return 0;
}
Bi 29:
#include <iostream>
using namespace std;
int main()
{
int gt=1,n;
cout<<"Nhap n=";
cin>>n;
for(int i = (n % 2)? 1 : 2; i <= n; i += 2) gt *= i;
cout<<n<<"!!= "<<gt<<endl;
return 0;
}
Bi 30:
#include <iostream> using namespace std;
#include <conio>
int main() {
int n,i;
long int S1=0,S2=0;
long double S3=1,S4=0;
12
Bi 39:
#include <iostream>
using namespace std;
void main()
{
for(int i=33;i<=255;i++)
cout<<i<<" : "<<(char) i<<"\n";
}
Bi 40:
#include <iostream>
using namespace std;
void main()
{
long tien,t500=0,t200=0,t100=0,t50=0,t20=0,t10=0,t5=0,t2=0,t1=0;
cout<<"Nhap so tien can doi : ";
cin>>tien;
if (tien>=500000)
{
t500=tien/500000;
tien=tien-t500*500000;
}
if (tien>=200000)
{
t200=tien/200000;
tien=tien-t200*200000;
}
if (tien>=100000)
{
t100=tien/100000;
tien=tien-t100*100000;
}
if (tien>=50000)
{
t50=tien/50000;
tien=tien-t50*50000;
}
if (tien>=20000)
{
15
t20=tien/20000;
tien=tien-t20*20000;
}
if (tien>=10000)
{
t10=tien/10000;
tien=tien-t10*10000;
}
if (tien>=5000)
{
t5=tien/5000;
tien=tien-t5*5000;
}
if (tien>=2000)
{
t2=tien/2000;
tien=tien-t2*2000;
}
if (tien>=1000)
{
t1=tien/1000;
tien=tien-t1*1000;
}
cout<<"Ban doi duoc \n"
<<t500<<" To 500k \n "
<<t200<<" To 200k \n "
<<t100<<" To 100k \n "
<<t50<<" To 50k \n "
<<t20<<" To 20k \n "
<<t10<<" To 10k \n "
<<t5<<" To 5k \n "
<<t2<<" To 2k \n "
<<t1<<" To 1k \n ";
}
Bi 53:
#include <iostream>
using namespace std;
bool ktNgTo(int );
16
void main()
{
int n;
cout<<"n= ";
cin>>n;
for (int i=2;i<=n;i++)
if (ktNgTo(i)) cout<<i<<" ";
}
bool ktNgTo(int n)
{
bool nt=true;
for (int i=2;i<n;i++)
{
if (n%i==0)
{
nt=false;
break;
}
}
////////////////////////////////
Max n s
# include <iostream>
using namespace std;
void main()
{
int day[200];
int sophantu;
do
{
cout<<"Nhap so phan tu cua mang ";
cin>>sophantu;
}
while ((sophantu<=0)|| (sophantu>200));
for (int i=1; i<=sophantu;i++)
{
cout<<"Nhap phan tu thu "<<i<<"= ";
17
cin>>day[i];
}
int max=day[1];
for (int i=1; i<=sophantu;i++)
{
if (max<day[i])
max = day[i];
}
cout<<max<<" la so lon nhat trong "<<sophantu<<" so da nhap \n";
}
///////////////////////////////////
V tam gic full
#include <iostream>
using namespace std;
void tamgiacrong(int);
void tamgiacrongnguoc(int) ;
void tamgiacdac(int);
void tamgiacdacnguoc(int);
void main()
{
int n;
cout<<"Nhap chieu cao tam giac : ";
cin>>n;
cout<<"--------Tam giac dac ------\n";
tamgiacdac(n);
cout<<"--------Tam giac dac nguoc ------\n";
tamgiacdacnguoc(n);
cout<<"--------Tam giac rong ------\n";
tamgiacrong(n);
cout<<endl;
cout<<"--------Tam giac rong nguoc ------\n";
tamgiacrongnguoc(n);
}
18
void tamgiacdac(int n)
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n-i;j++)
cout<<" ";
for (int j=1;j<=2*i-1;j++)
cout<<"*";
cout<<"\n";
}
}
void tamgiacdacnguoc(int n)
{
for (int i=n;i>=1;i--)
{
for (int j=1;j<=n-i;j++)
cout<<" ";
for (int j=1;j<=2*i-1;j++)
cout<<"*";
cout<<"\n";
}
}
void tamgiacrong(int n)
{
for (int i=1;i<=n-1;i++)
{
for (int j=1;j<=n-i;j++)
cout<<" ";
cout<<"*";
if (i>1)
{
for (int j=1;j<=2*i-3;j++)
cout<<" ";
cout<<"*";
}
19
cout<<"\n";
}
for (int i=1;i<=2*n-1;i++)
cout<<"*";
}
void tamgiacrongnguoc(int n)
{
for (int i=1;i<=2*n-1;i++)
cout<<"*";
cout<<"\n";
for (int i=n-1;i>=1;i--)
{
for (int j=1;j<=n-i;j++)
cout<<" ";
cout<<"*";
if (i>1)
{
for (int j=1;j<=2*i-3;j++)
cout<<" ";
cout<<"*";
}
cout<<"\n";
}
}
Bi 41:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
void nguoivsmay();
void mayvsnguoi();
void game();
void choitiep();
void win();
void lose();
20
void main()
{
system("cls");
game();
}
void nguoivsmay()
{
srand((unsigned)time(NULL));
int may=rand()%100;
bool dung=false;
int nguoichoi;
cout<<"Ban co 7 lan doan!!! CO LEN \n";
for (int i=1;i<=7;i++)
{
cout<<"-------------Lan doan thu "<<i<<"/7------------------------\n";
cout<<"Nhap so ban doan nao : ";
cin>>nguoichoi;
if (may>nguoichoi) cout<<"KO DUNG !!! .... So ban chon NHO QUA\n ";
if (may<nguoichoi) cout<<"KO DUNG !!! .... So ban chon LON QUA\n";
if (may==nguoichoi)
{
dung=true;
win() ;
break;
}
cout<<"-------------------------------------\n";
}
if (dung==false)
lose();
choitiep();
}
void mayvsnguoi()
{
bool dung=false;
srand((unsigned)time(NULL));
21
int nguoichoi,solan,may;
cout<<"Nhap so ban muon may doan ";
cin>>nguoichoi;
cout<<"Nhap so lan gioi han : ";
cin>>solan;
for (int i=1;i<=solan;i++)
{
may=rand()%100;
cout<<"Lan doan thu "<<setw(3)<<i<<" : May doan so "<<may<<endl;
if (may==nguoichoi)
{
dung=true;
lose();
break;
}
}
if (dung==false) win();
choitiep();
}
void game()
{
system("cls");
int chon;
cout<<"------------------ Rex Riddle -----------------------\n";
cout<<"******************************************************\n"
cout<<"************** TRO CHOI DOAN SO *******************\n"
cout<<"******************************************************\n"
cout<<endl;
cout<<"Vui long chon tro choi ^^ \n";
cout<<"1. Nguoi vs may ! \n";
cout<<"2. May vs nguoi ! \n";
cout<<"3. Thoat \n";
do
{
;
;
;
cin>>chon;
22
if ((chon<1)||(chon>3))
game();
}
while ((chon!=1)&&(chon!=2)&&(chon!=3));
system("cls");
if (chon==1) nguoivsmay();
if (chon==2) mayvsnguoi();
}
void win()
{
cout<<endl;
cout<<endl;
cout<<" ****
**** ****
*** ***\n";
cout<<" ***
*** ******** *** *** \n";
cout<<"
********** *** *** *** *** \n";
cout<<"
****
*** *** *** *** \n";
cout<<"
****
*** *** *** *** \n";
cout<<"
****
******** ********** \n";
cout<<"
****
****
********
\n";
cout<<endl;
cout<<" ***
******
*** **** ****** ***\n";
cout<<" *** *** *** *** **** *** *** ***\n";
cout<<" *** *** *** *** **** *** *** ***\n";
cout<<" *** ***
*** *** **** *** *** ***\n";
cout<<" ****
****
**** *** *******\n";
cout<<" ****
****
**** *** ******\n";
}
void lose()
{
cout<<endl;
cout<<endl;
cout<<" ****
****
****
***
***\n";
23
cout<<" ***
*** ******** *** *** \n";
cout<<"
********** *** *** *** *** \n";
cout<<"
****
*** *** *** *** \n";
cout<<"
****
*** *** *** *** \n";
cout<<"
****
******** ********** \n";
cout<<"
****
****
********
LOSE\n";
cout<<endl;
}
void choitiep()
{
char choitiep;
cout<<"Ban co muon choi tiep ko ?? (C/K) ";
cin>>choitiep;
if ((choitiep=='C')||(choitiep=='c')) main();
}
[i][i]
/////////////////////////////////////
on code trn c mt ci mi v phn to s ngu nhin....
Nhng tnh hung khng th d on trc c s lm nn s hp dn cho
game. Chng hn, s thay i chin thut bt ng ca i th trong game
dn trn, hoc s xut hin bt ng ca mt con qui vt trong action
game. To nn mt con s ngu nhin l mt k thut c bn ca dng ny.
Gii thiu chng trnh Die Roller: ( xc xc)
Chng trnh Die Roller ti hin li vic mt con xc xc c su mt. My tnh s tnh ton
mt no s xut hin mt cch ngu nhin. Hnh 2.11 cho thy kt qu ca chng trnh:
24
27