Description
After AC all the hardest problems in the world , the ACboy 8006 now has nothing to do . One day he goes to an old library to find a part-time job .It is also a big library which has N books and M users.The user's id is from 1 to M , and the book id is from 1 to N . According to the library rules , every user are only allowed to borrow 9 books .But what surprised him is that there is no computer in the library , and everything is just recorded in paper ! How terrible , I must be crazy after working some weeks , he thinks .So he wants to change the situation .
In the other hand , after 8006's fans know it , they all collect money and buy many computers for the library .
Besides the hardware , the library needs a management program . Though it is just a piece of cake for 8006 , the library turns to you , a excellent programer,for help .
What they need is just a simple library management program . It is just a console program , and have only three commands : Borrow the book , Return the book , Query the user .
1.The Borrow command has two parameters : The user id and the book id
The format is : "B ui bi" (1<=ui<=M , 1<=bi<=N)
The program must first check the book bi wether it's in the library . If it is not , just print "The book is not in the library now" in a line .
If it is , then check the user ui .
If the user has borrowed 9 books already, print "You are not allowed to borrow any more" .
Else let the user ui borrow the book , and print "Borrow success".
2.The Return command only has one parameter : The book id
The format is : "R bi" (1<=bi<=N)
The program must first check the book bi whether it's in the library . If it is , just print "The book is already in the library" . Otherwise , you can return the book , and print "Return success".
3.The Query command has one parameter : The user id
The format is : "Q ui" (1<=ui<=M)
If the number of books which the user ui has borrowed is 0 ,just print "Empty" , otherwise print the books' id which he borrows in increasing order in a line.Seperate two books with a blank.
Input
The input file contains a series of test cases . Please process to the end of file . The first line contains two integers M and N ( 1<= M <= 1000 , 1<=N<=100000),the second line contains a integer C means the number of commands(1<=C<=10000). Then it comes C lines . Each line is a command which is described above.You can assum all the books are in the library at the beginning of each cases.
Output
For each command , print the message which described above .
Please output a blank line after each test.
If you still have some questions , see the Sample .
Sample Input
5 10
9
R 1
B 1 5
B 1 2
Q 1
Q 2
R 5
Q 1
R 2
Q 1
5 10
9
R 1
B 1 5
B 1 2
Q 1
Q 2
R 5
Q 1
R 2
Q 1
Sample Output
The book is already in the library
Borrow success
Borrow success
2 5
Empty
Return success
2
Return success
Empty
The book is already in the library
Borrow success
Borrow success
2 5
Empty
Return success
2
Return success
Empty
#include <iostream>
using namespace std;
int main(){
int M,N;
while(cin>>M){
cin>>N;
int T,s;
cin>>T;
int y[M]={0},t[N],x[M][9]={0};
for(s=0;s<N;s++)
t[s]=1;
while(T--){
char a;
int b[2],c,d,i,j,k,temp;
cin>>a;
if(a=='B'){
for(i=0;i<2;i++)
cin>>b[i];
if(t[b[1]-1]==0)
cout<<"The book is not in the library now"<<endl;
else if(y[b[0]-1]>9)
cout<<"You are not allowed to borrow any more"<<endl;
else{
cout<<"Borrow success"<<endl;
t[b[1]-1]=0;
y[b[0]-1]++;
for(j=0;j<9;j++)
if(x[b[0]-1][j]==0){
x[b[0]-1][j]=b[1];
break;}}}
else if(a=='R'){
cin>>c;
if(t[c-1]==1)
cout<<"The book is already in the library"<<endl;
else{
t[c-1]=1;
cout<<"Return success"<<endl;
for(i=0;i<M;i++)
for(j=0;j<9;j++)
if(x[i][j]==c){
x[i][j]=0;
y[i]--;}}}
else{
cin>>d;
if(y[d-1]==0)
cout<<"Empty"<<endl;
else{
for(j=0;x[d-1][j+1]!=0;j++)
for(k=j+1;x[d-1][k]!=0;k++){
if(x[d-1][j]>x[d-1][k]){
temp=x[d-1][j];
x[d-1][j]=x[d-1][k];
x[d-1][k]=temp;}}
for(j=0;x[d-1][j]!=0&&j<9;j++)
cout<<x[d-1][j]<<' ';
cout<<endl;}}}}}
求大佬查錯
uj5u.com熱心網友回復:
至少是少輸出了一個空行要求是:
每個測驗用例之間還有有個空行
while(cin>>M) { // 和這個括號配對的} 之前還有有一個 cout << end;
。。。。。
cout<<endl;}}}; cout<<endl; }}
uj5u.com熱心網友回復:
沒想明白,你弄一個t陣列在哪兒做什么用?uj5u.com熱心網友回復:
t陣列來存書是否借走
uj5u.com熱心網友回復:
books[N]users[M][9]
兩個陣列就可以解決吧
books[bi] 如果是0就沒有被借走, 被借走就放ui
users[ui]記錄用戶ui借走的所有的書, 0表示空白, 其他的就bi了
借書就是users[ui] [x] = bi; books[bi] = ui,
還書就是books[bi] = 0; users[ui]里面把原來是bi的變成0的程序
查詢的時候對users[ui]排序后輸出
沒想明白為啥非要弄出三個陣列來呢
不寫注釋,變數名字也不起個有意義的名字,看著累死個人
uj5u.com熱心網友回復:
y記錄,用戶已經借走了幾本書了?意義不大, 只對已經是9的才有意義,否則還得一個一個的查詢陣列,看看哪一個是個空位好占上
這個優化作用幾乎起不到,用到的幾率太小了
uj5u.com熱心網友回復:
不是這個錯誤。。。。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/154425.html
標籤:C++ 語言
下一篇:麻煩一些大神教我這道題。
