#include <iostream>
using namespace std;
typedef struct String2Number
{
char *String;
int code;
};
String2Number auString2Number[] =
{
{"au",0x10},
{"bc",0x2},
{"chn",0x11},
{"dd",0x3},
{NULL,0},
{"zai",0x80}
};
bool IsEq(char *str1,char *str2)
{
char *p1 = str1;
char *p2 = str2;
while (*p1 == *p2 && *p1!='\0')
{
p1++;
p2++;
}
if (*p1 == '\0' && *p2 == '\0')return 0;
if (*p1 > *p2)return 1;
if (*p1 < *p2)return -1;
}
int GetCode(char *string)
{
int i = 0;
int j = sizeof(auString2Number) / sizeof(String2Number)-1;//统计元素个数。
while (i <= j)
{
int mid = i + (j - i) / 2;
if (IsEq(auString2Number[mid].String, string)==1)
{
j = mid - 1;
}
else if (IsEq(auString2Number[mid].String, string) == -1)
{
i = mid + 1;
}
else
{
return auString2Number[mid].code;
}
}
return -1;
}
int main()
{
cout << GetCode("au") << endl;
return 0;
}
笔试题:cvte第一题二分法求字符串
最新推荐文章于 2023-10-09 13:03:47 发布