Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

经过了无数次的WA之后,终于发现自己原来没注意到 put "title" first!这种情况! 贴代码,纪念啊!

Posted by kongsiyuan at 2011-08-13 12:16:54 on Problem 1886
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MAX 100000	//书的最大库存
#define SIZE 100

struct book
{
	char title[SIZE];
	char author[SIZE];
	int tag;	//tag=0表示在书架,tag=1表示借出,tag=2表示归还但是不在书架上
}b[MAX],r[MAX];

bool cmp(book b1,book b2)	//先根据author比较,如果author相同,再根据title比较
{
	if(strcmp(b1.author,b2.author)<0)
		return true;
	if(strcmp(b1.author,b2.author)==0)
	{
		if(strcmp(b1.title,b2.title)<0)
			return true;
	}

	return false;
}

int main()
{
	char tmp[200],tmp_title[200];
	int cnt=0;
	while(cin.getline(tmp,200)&&strcmp(tmp,"END")!=0)		//读取原来在书架上面的书
	{
		int i=1,j;
		while(tmp[i]!=char(34)&&tmp[i]!='\0')	//提取书名
		{
		//	cout<<"tmp[i]="<<tmp[i]<<" ";
			b[cnt].title[i-1]=tmp[i];
			i++;
		}
	//	cout<<b[cnt].title<<endl;
		for(i=i+5,j=0;i<strlen(tmp);i++,j++)	//提取作者名字
		{
			b[cnt].author[j]=tmp[i];
		}
		b[cnt].tag=0;
		//cout<<b[cnt].author<<endl;
	//	cout<<"cnt="<<cnt<<endl;
		cnt++;
		//cout<<endl;
	}
	sort(b,b+cnt,cmp);
	//for(int i=0;i<3;i++)	cout<<b[i].title<<endl;
	int k=0;
	while(cin.getline(tmp,200)&&strcmp(tmp,"END")!=0)	//判断是否为第二个END
	{
		int i,j;
		if(strcmp(tmp,"SHELVE")!=0)		//判断不是为SHELVE
		{
			//cout<<2<<endl;
			//cout<<tmp<<endl;
			for(i=8;tmp[i]!='"';i++)	//提取title
				tmp_title[i-8]=tmp[i];
			tmp_title[i-8]='\0';
			//cout<<tmp_title<<endl;
			if(tmp[0]=='B')		//借书
			{
				for(i=0;;i++)
				{
					if(strcmp(b[i].title,tmp_title)==0)
					{
						b[i].tag=1;
						break;
					}
					//cout<<i<<endl;
				}
				//cout<<5<<endl;
			}
			else	//还书
			{
				for(i=0;;i++)
				{
				//	cout<<"xx"<<endl;
					if(strcmp(b[i].title,tmp_title)==0)
					{
						b[i].tag=2;
						strcpy(r[k].title,b[i].title);
						strcpy(r[k].author,b[i].author);
						k++;
						//cout<<4<<endl;
						//cout<<"k="<<k<<endl;
						break;
					}
				}
			}
		}
		else		//SHELVE命令
		{
			//cout<<3<<endl;
			sort(r,r+k,cmp);	//对还的书进行排序
			for(i=0;i<k;i++)	
			{
				//cout<<"i="<<i<<endl;
				char bef_r[200];	//r[j]之前的书
				for(j=0;;j++)
				{
					if(b[j].tag==2&&strcmp(b[j].title,r[i].title)==0)
					{
						b[j].tag=0;
						break;
					}
				}
				j--;
				while(j>=0)
				{
					if(b[j].tag==0)
					{
						strcpy(bef_r,b[j].title);
						break;
					}
					j--;
				}
				if(j<0) cout<<"Put "<<char(34)<<r[i].title<<char(34)<<" first"<<endl;
				else cout<<"Put "<<char(34)<<r[i].title<<char(34)<<" after "<<char(34)<<bef_r<<char(34)<<endl;
			}
			cout<<"END"<<endl;
			k=0;
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator