HDU 1497 简单图书管理系统

没有算法简单程序题;

定义一个结构体node存储用户借的书和借的本数,数组book存书所有书被谁借了,未借出时是0;

#include
#include
#include
using namespace std;
int book[100005];
struct node{
	int have[10];
	int sum;
}p[1005];
int main(){
	int n,m,b,u;
	char a;
	while(cin>>n>>m){
		memset(book,0,sizeof(book));
		for(int i=0;i<=n;i++) p[i].sum=0;
		int t;
		cin>>t;
		while(t--){
			cin>>a;
			if(a=='B'){
			cin>>u>>b;
			if(book[b])	cout<<"The book is not in the library now"<<endl;	
			else if(p[u].sum>=9) cout<<"You are not allowed to borrow any more"<<endl;
			else{
			p[u].have[p[u].sum++]=b;
			book[b]=u;
			cout<<"Borrow success"<<endl; 
			} 
		}
		    else if(a=='R'){
		    	cin>>b;
		    	if(book[b]==0) cout<<"The book is already in the library"<<endl;
		    	else{
		    		int poit;
		    		u=book[b];
		    		book[b]=0;
		    		for(int i=0;i<p[u].sum;i++){
		    			if(p[u].have[i]==b){
		    				poit=i;
		    				break;
						}
					}
				for(int i=poit;i<p[u].sum-1;i++) p[u].have[i]=p[u].have[i+1];
				p[u].sum--;
				cout<<"Return success"<<endl;		
				}
				}
			else if(a=='Q'){
				cin>>u;
				if(p[u].sum==0) cout<<"Empty"<<endl;
				else{ 
				int b[10];
				for(int i=0;i<p[u].sum;i++)	b[i]=p[u].have[i];
				sort(b,b+p[u].sum);
				for(int i=0;i<p[u].sum;i++){
					if(i==0) cout<<b[i];
					else cout<<" "<<b[i];
				}
				cout<<endl;	
				
				}
			}	
			}
			cout<<endl;
	}
}

你可能感兴趣的:(HDU)