65,186
社区成员




#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef struct
{
char magic[25];
char func[85];
}NODE;
NODE words[100000+3];
NODE words2[100000+3];
bool cmp1(NODE a ,NODE b)
{
return strcmp(a.magic, b.magic) <0;
}
bool cmp2(NODE a,NODE b)
{
return strcmp(a.func, b.func) < 0;
}
//折半查找 搜索字符串
void search(char s[], int l , int r , int tag)
{
if( l > r)
{
printf("what?\n");
return ;
}
int m = (l+r)/2;
//magic --> fun
if( tag == 1)
{
if( strcmp(words[m].magic , s) < 0 )
search(s, m+1, r , tag);
else if(strcmp(words[m].magic , s) > 0 )
search(s, l , m-1, tag);
else
printf("%s\n",words[m].func);
}
else
{
if( strcmp(words2[m].func, s ) < 0)
search(s, m+1, r, tag);
else if(strcmp( words2[m].func, s) > 0 )
search(s, l , m-1, tag);
else
printf("%s\n",words2[m].magic);
}
}
int main()
{
char in[120];
char deal[120];
int t=0;
while( gets(in) && strcmp(in, "@END@") != 0)//
{
// if not, there will be a '\n' rest. and the result of 'scanf' will be 0
//fflush(stdin);
sscanf(in,"%[^]]",deal);
strcpy(words[t].magic, deal+1);
strcpy(words[t].func, in+strlen(deal)+2);
strcpy(words2[t].magic, deal+1);
strcpy(words2[t++].func, in+strlen(deal)+2);
}
//排序
sort(words, words+t,cmp1);
sort(words2,words2+t, cmp2);
int i,n;
scanf("%d",&n);
fflush(stdin);
while(n--)
{
gets(in);
if( in[0] == '[')
{
in[strlen(in)-1] = '\0';
search(in+1, 0,t-1,1);
}
else
{
search(in, 0,t-1,2);
}
}
return 0;
}
getchar();//改 fflush(stdin);
printf("%s\n",(*q).func); //改 printf("%s,\n",(*q).func);
printf("%s\n",(*q).magic); //改 printf("%s,\n",(*q).magic);
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
while (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
while (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char magic[25];
char func[85];
}NODE;
NODE words[100000+3];
NODE words2[100000+3];
int cmp1(const void *p, const void *q)
{
return strcmp( (*(NODE *)p).magic, (*(NODE *)q).magic);
}
int cmp2(const void *p,const void *q)
{
return strcmp( (*(NODE *)p).func, (*(NODE *)q).func );
}
int main()
{
char in[120];
char deal[120];
int t=0;
while( gets(in) && strcmp(in, "@END@") != 0)//
{
// if not, there will be a '\n' rest. and the result of 'scanf' will be 0
//fflush(stdin);
sscanf(in,"%[^]]",deal);
strcpy(words[t].magic, deal+1);
strcpy(words[t].func, in+strlen(deal)+2);
strcpy(words2[t].magic, deal+1);
strcpy(words2[t++].func, in+strlen(deal)+2);
}
//排序
qsort( words, t, sizeof(NODE), cmp1);
qsort( words2, t , sizeof(NODE), cmp2);
int i,n;
NODE *q,temp;
scanf("%d",&n);
fflush(stdin);
while(n--)
{
gets(in);
if( in[0] == '[')
{
in[strlen(in)-1] = '\0';
strcpy(temp.magic,in+1);
q = (NODE *)bsearch(&temp,words, t, sizeof(NODE), cmp1);
if( q== NULL)
{
printf("what?\n");
}
else
{
printf("%s,\n",(*q).func);
}
}
else
{
strcpy(temp.func, in);
q = (NODE *)bsearch(&temp, words2, t ,sizeof(NODE), cmp2);
if( q== NULL)
{
printf("what?\n");
}
else
{
printf("%s,\n",(*q).magic);
}
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char magic[25];
char func[85];
}NODE;
NODE words[100000+3];
NODE words2[100000+3];
int cmp1(const void *p, const void *q)
{
return strcmp( (*(NODE *)p).magic, (*(NODE *)q).magic);
}
int cmp2(const void *p,const void *q)
{
return strcmp( (*(NODE *)p).func, (*(NODE *)q).func );
}
int main()
{
char in[120];
char deal[120];
int t=0;
while( gets(in) && strcmp(in, "@END@") != 0)//
{
// if not, there will be a '\n' rest. and the result of 'scanf' will be 0
//fflush(stdin);
sscanf(in,"%[^]]",deal);
strcpy(words[t].magic, deal+1);
strcpy(words[t].func, in+strlen(deal)+2);
strcpy(words2[t].magic, deal+1);
strcpy(words2[t++].func, in+strlen(deal)+2);
}
//排序
qsort( words, t, sizeof(NODE), cmp1);
qsort( words2, t , sizeof(NODE), cmp2);
int i,n;
NODE *q;
scanf("%d",&n);
fflush(stdin);
while(n--)
{
gets(in);
if( in[0] == '[')
{
in[strlen(in)-1] = '\0';
q = (NODE *)bsearch(in+1,words, t, sizeof(NODE), cmp1);
//printf("%0x,",q);
if( q== NULL)
{
printf("what?\n");
}
else
{
printf("%s\n",(*q).func);
}
}
else
{
q = (NODE *)bsearch(in, words2, t ,sizeof(NODE), cmp2);
if( q== NULL)
{
printf("what?\n");
}
else
{
printf("%s\n",(*q).magic);
}
}
}
return 0;
}