| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 587 人关注过本帖
标题:关于散列表插队买票问题
只看楼主 加入收藏
ou461560374
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-4-8
收藏
 问题点数:0 回复次数:0 
关于散列表插队买票问题
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define inf 1000005
#define NULL 0
int n,m,pt;
struct node{
 char name[50];
 int group;
}p[inf];
struct qu{
 char na[50];
 int group;
 qu *next;
};
int find(char tmp[]){
 int hi=pt-1,lo=0,mid;
 while(lo<=hi){
  mid=(hi+lo)>>1;
  if(strcmp(tmp,p[mid].name)==0) return p[mid].group;
  if(strcmp(tmp,p[mid].name) >0 ) lo=mid+1;
  else hi=mid-1;
 }return p[mid].group;
}
void insert(qu *head,int g,char tmp[]){
 qu *add=new qu;
 strcpy(add->na,tmp), add->group=g;
 if(head->next==NULL){
  head->next=add, add->next=NULL; return;
 }
 qu *t=head->next;
 while(t->next!=NULL){
  if(t->group == g && t->next->group!=g){
   add->next=t->next;
   t->next=add; return;
  }else t=t->next;
 }
 if(t->next == NULL){
   t->next=add, add->next=NULL; return;
  }
}
void out(qu *head){
 qu *t=head->next;
 printf("%s\n",t->na);
 head->next=t->next;
 delete t;
}
void solve(){
 char tmp[50];
 int g;
 qu *head=new qu;
 head->next=NULL;
 while(scanf("%s",tmp)){
  if(strcmp(tmp,"ENQUEUE") == 0){
   scanf("%s",tmp);
   g=find(tmp);
   insert(head,g,tmp);
   qu *t=head->next;
  }
  else if(strcmp(tmp,"DEQUEUE") ==0) out(head);
  else break;
 }
}
bool cmp(node a,node b)
{return ( strcmp(a.name,b.name) < 0 );}
int main(){
 int i,j,cas=1;
 while(scanf("%d",&n) && n){
  printf("Scenario #%d\n",cas++);
  pt=0;
  for(i=0;i<n;i++){
   scanf("%d",&m);
   for(j=0;j<m;j++){
    scanf("%s",p[pt].name);
    p[pt++].group=i;
   }
  }
  sort(p,p+pt,cmp);
  solve();
  printf("\n");
 }
 return 0;
}
哪位好心人给点注释。实在看不懂
搜索更多相关主题的帖子: mid 买票 include return insert 
2012-04-08 17:05
快速回复:关于散列表插队买票问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017173 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved