思路:定义一个结构体来存书,重点在格式化输入。本来不想用string的,但是发现char*还是麻烦,就用了string,最后结果也没有多慢,最后一个测试用例270ms,在没用map和set优化的情况下还可以了。。。
如果会熟练使用map和set,可以参考其他人的博客,有更快的算法。
做的时候遇到了几个坑,首先我一开始用的是qsort函数,但是对我这个结构体排序完之后,里面的string居然丢失数据了!用sort就没事了。如果有大佬懂的话,希望可以点拨一下~
第二个坑是fflush这个函数,这个不是c/c++标准里面的,最好不要用了。想吸收每行输入结束的回车符,还是用getchar()吧。
AC代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <sstream>
#include <string.h>
#include <algorithm>
using namespace std;
struct Book{
int id;
string title;
string author;
vector<string> keywords;
string publisher;
int year;
};
Book *book;
int N,M;
string *query;
bool cmp(Book _a,Book _b)
{
//Book *a = (Book*)_a;
//Book *b = (Book*)_b;
return _a.id < _b.id;
}
void Sear(int x,int index)
{
int booknum = 0;
switch(x)
{
case 1:
for(int r = 0;r<N;r++)
{
if(book[r].title == query[index])
{
printf("\n%07d",book[r].id);
booknum++;
}
}
break;
case 2:
for(int r = 0;r<N;r++)
{
if(book[r].author == query[index])
{
printf("\n%07d",book[r].id);
booknum++;
}
}
break;
case 3:
for(int r = 0;r<N;r++)
{
for(int g = 0;g<book[r].keywords.size();g++)
{
if(book[r].keywords[g] == query[index])
{
printf("\n%07d",book[r].id);
booknum++;
break;
}
}
}
break;
case 4:
for(int r = 0;r<N;r++)
{
if(book[r].publisher == query[index])
{
printf("\n%07d",book[r].id);
booknum++;
}
}
break;
case 5:
for(int r = 0;r<N;r++)
{
if(book[r].year == atoi(query[index].c_str()))
{
printf("\n%07d",book[r].id);
booknum++;
}
}
break;
}
if(booknum == 0)
{
printf("\nNot Found");
}
}
int main()
{
scanf("%d",&N);
book = new Book[N];
char temp[80];
string s;
char c;
for(int i = 0;i<N;i++)
{
scanf("%d",&book[i].id);
//fflush(stdin);
c = getchar();
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
book[i].title = temp;
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
book[i].author = temp;
//cout<<book[i].author<<endl;
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
s = temp;
stringstream ss(s);
string x;
while(ss>>x)
{
book[i].keywords.push_back(x);
x = ""; //清空字符串x
}
//cout<<book[i].keywords[0]<<book[i].keywords[1];
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
book[i].publisher = temp;
scanf("%d",&book[i].year);
}
//cout<<book[0].author<<endl<<book[1].author<<endl<<book[2].author<<endl;
sort(book,book+N,cmp);
//cout<<book[0].author<<endl<<book[1].author<<endl<<book[2].author<<endl;
scanf("%d",&M);
query = new string[M];
int num;
for(int j = 0;j<M;j++)
{
scanf("%d: ",&num);
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
query[j] = temp;
cout<<num<<": "<<query[j];
Sear(num,j);
if(j!=M-1)
{
printf("\n");
}
}
return 0;
}
下面这个是第一次写的代码,排序后输出author,发现出错了
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <sstream>
#include <string.h>
#include <algorithm>
using namespace std;
struct Book{
int id;
string title;
string author;
vector<string> keywords;
string publisher;
int year;
};
Book *book;
int N,M;
string *query;
int cmp(const void* _a,const void* _b)
{
Book *a = (Book*)_a;
Book *b = (Book*)_b;
return a->id > b->id;
}
void Sear(int x,int index)
{
int booknum = 0;
switch(x)
{
case 1:
for(int r = 0;r<N;r++)
{
if(book[r].title == query[index])
{
printf("\n%07d",book[r].id);
booknum++;
}
}
break;
case 2:
for(int r = 0;r<N;r++)
{
if(book[r].author == query[index])
{
printf("\n%07d",book[r].id);
booknum++;
}
}
break;
case 3:
for(int r = 0;r<N;r++)
{
for(int g = 0;g<book[r].keywords.size();g++)
{
if(book[r].keywords[g] == query[index])
{
printf("\n%07d",book[r].id);
booknum++;
break;
}
}
}
break;
case 4:
for(int r = 0;r<N;r++)
{
if(book[r].publisher == query[index])
{
printf("\n%07d",book[r].id);
booknum++;
}
}
break;
case 5:
for(int r = 0;r<N;r++)
{
if(book[r].year == atoi(query[index].c_str()))
{
printf("\n%07d",book[r].id);
booknum++;
}
}
break;
}
if(booknum == 0)
{
printf("\nNot Found");
}
}
int main()
{
scanf("%d",&N);
book = new Book[N];
char temp[80];
string s;
char c;
for(int i = 0;i<N;i++)
{
scanf("%d",&book[i].id);
//fflush(stdin);
c = getchar();
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
book[i].title = temp;
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
book[i].author = temp;
//cout<<book[i].author<<endl;
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
s = temp;
stringstream ss(s);
string x;
while(ss>>x)
{
book[i].keywords.push_back(x);
x = ""; //清空字符串x
}
//cout<<book[i].keywords[0]<<book[i].keywords[1];
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
book[i].publisher = temp;
scanf("%d",&book[i].year);
}
//cout<<book[0].author<<endl<<book[1].author<<endl<<book[2].author<<endl;
qsort(book,N,sizeof(Book),cmp);
cout<<book[0].author<<endl<<book[1].author<<endl<<book[2].author<<endl;
scanf("%d",&M);
query = new string[M];
int num;
for(int j = 0;j<M;j++)
{
scanf("%d: ",&num);
scanf("%[^\n]",temp);
//fflush(stdin);
c = getchar();
query[j] = temp;
cout<<num<<": "<<query[j];
Sear(num,j);
if(j!=M-1)
{
printf("\n");
}
}
return 0;
}
测试数据:(Yue后面的Chen被吃了,并且没有正确按照id大小排序)

210



被折叠的 条评论
为什么被折叠?



