mình có 2 bài toán:
Cài đặt thuật giải tìm kiếm tuyến tính và tìm kiếm nhị phân trên danh sách n số nguyên trả kết quả tìm được về True & False.Nhưng chả hiểu sao lúc trả về dù tìm được vẫn là false.
Đây là code:
Code:
//Viet tren nen Cfree4.1
#include<iostream.h>
struct DAY{
int n;
int key[50];
};
/////////////
void Nhap(DAY&A)
{
while(A.n>50)
{
cout<<"Nhap vao so phan tu cua day:";
cin>>A.n;
}
cout<<"******NHAP VAO*******\n";
for(int i=0;i<A.n;i++)
{
cout<<"Phan tu thu: "<<i+1<<"\n";
cin>>A.key[i];
}
}
/////////////
void Xuat(DAY A)
{
cout<<"******DAY DA NHAP VAO LA******\n";
for(int i=0;i<A.n;i++)
{
cout<<A.key[i]<<" ";
}
cout<<"\n";
}
/////////////
int LinearSearch(DAY A,int x)
{
cout<<"Nhap x: ";cin>>x;
int i=0;
A.key[50]=x;
while(A.key[i]!=x)
i++;
if(i==A.n)
return 0;
else
return 1;
}
/////////////
void SxTang(DAY &A)
{
int temp;
for(int i=0;i<A.n-1;i++)
{
for(int j=i+1;j<A.n;j++)
{
if(A.key[i]>A.key[j])
{
temp=A.key[i];
A.key[i]=A.key[j];
A.key[j]=temp;
}
}
}
Xuat(A);
}
/////////////
int BinarySearch(DAY A,int Y)
{
SxTang(A);
cout<<"Nhap vao Y: ";
cin>>Y;
int left=0;
int right=A.n-1;
int mid;
do{
mid=(left+right)/2;
if(Y=A.key[mid]) return 1;
else
if(Y<A.key[mid]) right=mid-1;
else
left=mid+1;
}while(left<=right);
return 0;
}
/////////////
void main()
{
int key[50];
int x,Y,n,w,a;
DAY A;
do{
cout<<"\nHay chon thao tac:\n 1. Nhap day\n 2. Xuat day\n 3. Tim kiem tuyen tinh tra ve true false\n";
cout<<" 4. Sap xep tang \n 5. Tim kiem nhi phan tra ve true false\n";
cin>>a;
switch(a)
{
case 1:Nhap(A);break;
case 2:Xuat(A);break;
case 3:
if(LinearSearch(A,x)==0)
cout<<"TRUE\n";
else
cout<<"FALSE\n";
break;
case 4:SxTang(A);break;
case 5:
if(BinarySearch(A,Y)==0)
cout<<"TRUE\n";
else
cout<<"FALSE\n";
break;
default : cout<<"CHI DUOC CHON TU 1 DEN 4!!!";
}cout<<"Tiep tuc ko?{An -1 de thoat}";
cin>>w;
}while(w!=-1);
}
Và 1 bài nữa:
Định nghĩa cấu trúc phân số. Viết chương trình:
Nhập, xuất danh sách n phân số
Tìm kiếm phân số X trên danh sách. Nếu có cho biết vị trí của X trong danh sách. Mình định tìm kiếm theo giá trị của nó. Nhưng hàm rút gọn bị lỗi gì đó mình chả hiểu nữa :(
Code:
Code:
#include<iostream.h>
#include<math.h>
struct PS{
int tu,mau;
};
struct DAYPS{
int n;
PS key[50];
};
/////////////
void NhapPS(PS&P)
{
cout<<"Nhap tu so: ";
cin>>P.tu;
cout<<"Nhap mau so: ";
cin>>P.mau;
}
/////////////
void XuatPS(PS P)
{
cout<<P.tu<<"/"<<P.mau<<" ";
}
/////////////
void NhapDayPS(DAYPS&D)
{
cout<<"Nhap so phan tu cua day: ";
cin>>D.n;
for(int i=0;i<D.n;i++)
{
NhapPS(D.key[i]);
}
}
/////////////
void XuatDayPS(DAYPS&D)
{
cout<<"Day phan so sau khi duoc rut gon la:";
for(int i=0;i<D.n;i++)
{
cout<<RutGonPS(D.key[i])<<" ";//loi o day
}
}
/////////////
int UCLN(PS P)
{
if(P.tu==P.mau)
return P.tu;
P.tu=abs(P.tu);
P.mau=abs(P.mau);
while(P.tu!=P.mau)
{
if(P.tu>P.mau)
P.tu=P.tu-P.mau;
else
P.mau=P.mau-P.tu;
}
return P.tu;
}
/////////////
PS RutGon(PS&P)
{
P.tu=P.tu/UCLN(P);
P.mau=P.mau/UCLN(P);
return P;
}
/////////////
int LinearSearch()
{
}
////////////
void BinearSearch()
{
}
/////////////
void main()
{
PS key[50];
PS P;
DAYPS D;
int w,k;
do{
cout<<"*****CHON THAO TAC*****";
cout<<"\n 1.Nhap vao day phan so\n 2.Xuat ra day phan so\n";
cin>>k;
switch(k)
{
case 1: NhapDayPS(D);break;
case 2: XuatDayPS(D);break;
default:"Chon lai di ban^^";
}
cout<<"Tiep tuc?(An -1 de out)\n";cin>>w;
}
while(w!=-1);
}
TKS mọi người 