Câu 1:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
struct TaiSan
{
char maTS[30];
char tenTS[30];
int soLuong;
int namMua;
};
struct Node
{
TaiSan info;
Node *pnext;
};
typedef struct Node *TRO;
void nhap1TS(TaiSan
&ts)
{
fflush(stdin);
cout<<"Nhap Ma Tai San:
"<<endl;
gets(ts.maTS);
fflush(stdin);
cout<<"Nhap ten Tai San:
"<<endl;
gets(ts.tenTS);
cout<<"Nhap so luong:
"<<endl;
cin>>ts.soLuong;
cout<<"nhap nam mua:
"<<endl;
cin>>ts.namMua;
}
void ADD_LAST(TRO
&l, TaiSan x)
{
Node *p = new Node;
p->info = x;
p->pnext = NULL;
if(l==NULL)
{
l = p;
}
else
{
TRO q = l;
while(q->pnext!=NULL)
{
q = q->pnext;
}
q->pnext = p;
}
}
void hienThi1TaiSan(TaiSan
ts)
{
cout<<endl;
cout<<ts.maTS<<endl;
cout<<ts.tenTS<<endl;
cout<<ts.soLuong<<endl;
cout<<ts.namMua<<endl;
}
void HienThiList(TRO
l)
{
while(l!=NULL)
{
if(l->info.namMua<2009)
{
hienThi1TaiSan(l->info);
}
l = l->pnext;
}
}
void insertTaiSan(int
vt, TaiSan ts, TRO &l)
{//vị
trí của mình đếm từ 0
TRO q = new Node;
q->info = ts;
TRO p = l;
int i =
0;
while(p->pnext!=NULL)
{
if(i==vt-1)
{
q->pnext = p->pnext;
p->pnext = q;
break;
}
i++;
p = p->pnext;
}
}
void nhapNhieuTaiSan(TRO
&l)
{
while(true)
{
TaiSan trunggian;
nhap1TS(trunggian);
if(strcmp(trunggian.maTS,"")==0)
{
break;
}
else
{
ADD_LAST(l, trunggian);
}
}
}
void Create(TRO
&l)
{
l = NULL;
}
int main()
{
TRO l;
Create(l);
nhapNhieuTaiSan(l);
HienThiList(l);
cout<<"Nhap TAI SAN CAN CHEN:
"<<endl;
TaiSan taisancanchen;
nhap1TS(taisancanchen);
insertTaiSan(4,taisancanchen,l);
cout<<"SAU KHI CHEN
:"<<endl;
HienThiList(l);
return 0;
}
-----------------------------------------------------
Câu 2:
AB-C/A-DE+-B+
Step1
A|B|
-
A-B = X
Step2
|X|C|
/
X/C = Y
Step 3
|Y|A|
-
Y – A = Z
Step 4
|Z|D|E
+
D+ E = G
Step 5
Z|G|
-
Z-G = I
Step6
|I|B|
+
+
I+B = KQ
_-------------------------------------------------------------------------------------
_-------------------------------------------------------------------------------------
Câu 3:
Bài giải
//Mã Nguồn
#include <iostream>
using namespace std;
void prinArray(int
a[], int n)
{
for(int i =0;i<n;i++)
cout<<a[i]<<"\t";
}
void SapXepNoiBot(int a[], int n)
{
for(int i = 1;i<n;i++)
{
for(int j = 0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
swap(a[j],a[j+1]);
}
}
}
}
int timNhiPhan(int
a[], int L, int R, int k)
{
cout<<L<<"
"<<R<<" -- ";
int MID
= (R+L)/2;
cout<<MID<<"
"<<a[MID]<<"\n";
if(L>R)
{
return 0;
}
if(k ==
a[MID])
return 1;
else if(a[MID]
< k)
{
return timNhiPhan(a,
MID+1,R,k);
}
else if(a[MID]
> k)
{
return timNhiPhan(a,
L,MID-1,k);
}
}
int main()
{
int a[]
= {15,10,22,28,27,35,34};
int n =
7;
SapXepNoiBot(a,n);
prinArray(a,n);
cout<<"\nabc\n";
cout<<timNhiPhan(a,0,n-1,19);
return 0;
}
0 nhận xét:
Post a Comment