以下四题均通过调试,运行并得到正确结果
******************第一题**********************************************
#include
#define N 20
using namespace std;
void main()
{char s1[N],s2[N];
char *p=s1,*q=s2,*temp; //定义指针p ,q分别遍历s1,s2以及临时指针temp
cout<<"Input the string s1:"<
cout<<"Input the string s2:"<
for(;*p;p++)
{ for(q=s2;*q;q++)
{ if(*p==*q) //如果S1存在字符在S2中则删除该字符并退出循环遍历下个字符
{for(temp=p;*temp;temp++)
*temp=*(temp+1); //s1中删除该字符,所有字符前移一位
p--; //删除字符并移动后,p指向新的字符,结束循环p会加1,所以这里一定要减一
break;
}
else continue;
}
}
cout<<"the new s1 is:"<
cout<<*p;
}
********************第二题************************************************
#include
#include
#define N 10
using namespace std;
void main()
{ char str[N];
int i;
cout<<"Please input the string:"<
cout<
cout<
****************第三题*****************************************
#include
#define N 100
using namespace std;
void main()
{int a[N],count,i,j,temp;
cout<<"Input the number of the data:"<
cout<<"Input the data:"<
for(i=0;i
a[i]=a[j];
a[j]=temp;
}
else continue;
}
}
cout<<"The result is:"<
***************第四题******************************************
#include
#define N 100
using namespace std;
int Search(int *T,int key,int lengh)
{int low,high,mid;
low=1;
high=lengh;
while(low<=high)
{mid=(low+high)/2;
if(*(T+mid)==key) return mid; //找到查找元素
else if(key<*(T+mid)) high=mid-1; //继
续在前半区间查找
else low=mid+1; //继续在后半区间查找
}
return 0; //找不到待查的元素
}
void main()
{int a[N],count,result,key,i;
cout<<"Input the number of the data:"<
cout<<"Input the data(low-->high):"<
cout<<"Input the key:"<
result=Search(a,key,count);
if(result==0) cout<<"Can't find the key"<
第四题补充好了。。 if(x>p[mid]) former=mid;
第一题:
#include
#include
using namespace std;
string s1,s2;
void FuncOne(int m)
{
int i;
for(i=m;i
s1[i]='\0';
}
void FuncTwo(char ch)
{
int i;
for(i=0;i
}
int main()
{
cout<<"s1?\t";
getline(cin,s1);
cout<<"s2?\t";
getline(cin,s2);
int i,j;
for(i=0;i
cout<
}
第二题:
#include
#include
using namespace std;
int main()
{
string s;
cout<<"字符串: "; getline(cin,s);
char ch;
int j=s.size();
--j;
for(int i=0;i
ch=s[i];
s[i]=s[j];
s[j]=ch;
}
cout< return 0;
}
第三题:
#include
using namespace std;
void sort(int *p,int m)
{
int i,j,t;
for(i=0;i
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
int main()
{
int a[100],i,count;
cout<<"您想输入几个数?";
cin>>count;
for(i=0;i
cout<<"排序前:"<
cout<
cout<<"排序后:"<
cout<
}
第四题:
#include
using namespace std;
void sort(int p[],int n)
{
int i,j,t;
for(i=0;i
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
bool Func(int p[],int n,int x,int &offset)
{
int mid,former,latter;
former=0;
latter=n;
mid=(former+latter)/2;
while(former!=latter-1)
{
if(x==p[mid]) {offset=mid;return true;}
if(x
mid=(former+latter)/2;
}
return false;
}
int main()
{
int i,n,x,a[80],offset;
bool Found;
cout<<"您想输入几个数? ";
cin>>n;
cout<<"请输入"<
cout<<"您想查找的数?\t";
cin>>x;
cout<<"正在转换成有序数列……"<
for(i=0;i
cout<<"\n二分法查找……\n";
Found=Func(a,n,x,offset);
if(!Found)
{cout<<"对不起,没有找到元素 "<
}
cout<<"您要查找的元素 "<
return 0;
}
第四题:
#include
void nfFind(int DataArray[],int nSize,int fVale)
{
int flag=0;
int nStart=0;
int nEnd=nSize-1;
int nMid;
while(nStart<=nEnd)
{
nMid=(nStart+nEnd)/2;
if(DataArray[nMid]==fVale)
{
flag=1;
break;
}
if(fVale>DataArray[nMid])
nStart=nMid+1;
else
nEnd=nMid-1;
}
if(flag)
cout<<"元素找到了,是第"<
cout<<"在数组中没有要找的元素!"<
void main()
{
cout<<"请输入数组元素的个数:";
int n;
cin>>n;
int *a=new int[n];
cout<<"请输入有序数组:"<
cout<<"请输入你要查找的数:";
int fv;
cin>>fv;
nfFind(a,n,fv);
}