HDU 1497(Simple Library Management System)

spoiler posted @ 2011年4月16日 02:27 in 各种比赛题目收集 , 1345 阅读

题目大意:图书管管理系统,有三个操作,一个借书,还书,查询。

优化方案:就是一个数组存放书的信息,下标为书的代号,内容存,0或者借走这本书的同学学号,这样可以方便查询!

#include<stdio.h>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int stu,n;

struct node{
	int num;
	int t[15];
}s[1010];

int book[100005];

void init(){
	for(int i=1;i<=stu;i++){
		memset(s[i].t,0,sizeof(s[i].t));
		s[i].num=0;
	}
	memset(book,0,sizeof(book));
}
int cmp(int x,int y){
	return x<y;
}

int main(){
	int i,j,times;
	int x,y;
	char ch;
	while(~scanf("%d %d",&stu,&n)){
		scanf("%d",×);
		init();
		while(times--){
			j=0;
			getchar();
			scanf("%c",&ch);
			if(ch=='R'){
				scanf("%d",&x);
				if(book[x]!=0){
					printf("Return success\n");
					for(i=1;i<=9;i++){
						if(s[book[x]].t[i]==x)
							s[book[x]].t[i]=0;	
					}				
					s[book[x]].num--;
					book[x]=0;			
				}
				else
					printf("The book is already in the library\n");
				
			}
			else if(ch=='B'){
				scanf("%d %d",&x,&y);
				if(book[y]!=0){
                                 //这个地方一定得注意!因为这个WA了N次,题目说了
                                 //这个有先后顺序的,先判断书在么,再判断该同学借了9本了没
					printf("The book is not in the library now\n");
				}
				else if(s[x].num==9){
					printf("You are not allowed to borrow any more\n");
				}
				else{
					printf("Borrow success\n");
					sort(s[x].t,s[x].t+10,cmp);
					s[x].t[1]=y;
					s[x].num++;
					book[y]=x;
				}
					
			}
			else{
				scanf("%d",&x);
				sort(s[x].t,s[x].t+10,cmp);
				if(s[x].num==0)
					printf("Empty");
				//cout<<"???"<<endl;
				if(s[x].num!=0){
					for(i=1;i<=9;i++)
						if(s[x].t[i]!=0){
							printf(j==0?"%d":" %d",s[x].t[i]);
							j++;
						}
				}
				printf("\n");
			}
		}
		printf("\n");
		
	}
	return 0;
}

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter