题意:模拟图书馆借书
book用于记录 该书 被那个学生借走了
pos 用于记录这本书被借走的同学放在了第几位.
注意:每次还书 的时候要更新pos!!!!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<queue> 5 #include<stdlib.h> 6 #include<algorithm> 7 using namespace std; 8 const int maxn = 100005; 9 const int maxm = 1005; 10 int book[ maxn ],pos[ maxn ]; 11 //int vis[ maxn ]; 12 struct node{ 13 int book[ 12 ]; 14 int cnt; 15 }stu[ maxm ]; 16 int main(){ 17 int m,n; 18 while( scanf("%d%d",&m,&n)!=EOF ){ 19 for( int i=0;i<=m;i++ ){ 20 stu[ i ].cnt=0; 21 } 22 memset( book,-1,sizeof( book ));//book[ i ] the i people 23 memset( pos,-1,sizeof( pos ));// pos[ k ] the kth place 24 int ca; 25 scanf("%d",&ca); 26 for( int t=1;t<=ca;t++ ){ 27 char tmp[5]; 28 scanf("%s",&tmp); 29 if( tmp[0]=='B' ){ 30 int u,b; 31 scanf("%d%d",&u,&b); 32 if( book[ b ]!=-1 ){ 33 printf("The book is not in the library now\n"); 34 } 35 else if( stu[ u ].cnt>=9 ){ 36 printf("You are not allowed to borrow any more\n"); 37 } 38 else { 39 stu[ u ].book[ stu[u].cnt ]=b; 40 pos[ b ]=stu[ u ].cnt; 41 book[ b ]=u; 42 stu[ u ].cnt++; 43 printf("Borrow success\n"); 44 } 45 } 46 else if( tmp[0]=='R' ){ 47 int b; 48 scanf("%d",&b); 49 if( book[ b ]==-1 ){ 50 printf("The book is already in the library\n"); 51 } 52 else { 53 printf("Return success\n"); 54 for( int j=pos[b];j<stu[book[b]].cnt-1;j++ ){ 55 stu[ book[b] ].book[ j ]=stu[ book[b] ].book[ j+1 ]; 56 pos[ stu[ book[b] ].book[ j+1 ] ]=j; 57 } 58 59 stu[ book[b] ].cnt--; 60 book[ b ]=-1; 61 pos[ b ]=-1; 62 63 } 64 } 65 else if( tmp[0]=='Q' ){ 66 int u; 67 scanf("%d",&u); 68 if( stu[ u ].cnt==0 ){ 69 printf("Empty\n"); 70 } 71 else { 72 int temp[ 12 ]; 73 int k=0; 74 for( int i=0;;i++ ){ 75 if( pos[stu[u].book[ i ]]!=-1 ){ 76 temp[ k++ ]=stu[u].book[i]; 77 if( k>=stu[u].cnt ) break; 78 } 79 } 80 sort( temp,temp+k ); 81 for( int i=0;i<k;i++ ){ 82 if( i==0 ) printf("%d",temp[i]); 83 else printf(" %d",temp[i]); 84 } 85 printf("\n"); 86 } 87 } 88 } 89 printf("\n"); 90 } 91 return 0; 92 }