蜂蜜和白醋能减肥么:急!!C++题目(万分感激)

来源:百度文库 编辑:高考问答 时间:2024/04/29 20:30:25
定义一个10人的人员名单(人员的姓名自己假设),将此名单排序,并利用二分查找法查找用户指定的人员,
若找到则显示排序后的人员名单及此人在名单中的序号,否则提示用户未找到此人。
请用两种方法实现:a) 使用二维字符数组 b) 使用string类字符串数组

#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#define STRCMP(a, R, b) (strcmp(a, b) R 0)

using namespace std;

bool sortCharStar(char a[][80],const int n)
{
int i,j;
char tmp[80];
for(i=1;i<n;++i)
for(j=0;j<n-i;++j)
if(STRCMP(a[j],>,a[j+1]))
{
strcpy(tmp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],tmp);
}
}

int BinarySearchCharStar(char a[][80],const char x[],const int n)
{
int left=0;
int right=n-1;
while(left<=right){
int middle=(left+right)/2;
if (STRCMP(x,==,a[middle])) return middle;
if (STRCMP(x,>,a[middle])) left=middle+1;
else right=middle-1;
}
return -1;
}

int BinarySearchString(string a[],const string & x,const int n)
{
int left=0;
int right=n-1;
while(left<=right){
int middle=(left+right)/2;
if (x==a[middle]) return middle;
if (x>a[middle]) left=middle+1;
else right=middle-1;
}
return -1;
}

int main()
{
char a[10][80]={"j","i","h","g","f","e","d","c","b","a"};
string b[10]={"j","i","h","g","f","e","d","c","b","a"};
char ina[80];
string inb;
int res;

/*char[]实现*/
sortCharStar(a,10);
cin>>ina;
res=BinarySearchCharStar(a,ina,10);
if(res==-1)
cout<<"There is no such person."<<endl;
else
cout<<"His name is "<<ina<<" and he is No."<<res<<endl;

/*string实现*/
sort(b,b+10);
cin>>inb;
res=BinarySearchString(b,inb,10);
if(res==-1)
cout<<"There is no such person."<<endl;
else
cout<<"His name is "<<inb<<" and he is No."<<res<<endl;
return 0;
}

恩 你都5级拉都不会

我们刚学完 这个查找方法 !!但是没有听棵 哈哈

等哪天我会了在告诉你