1022. Digital Library (30)

心已赠人 2022-05-31 10:10 255阅读 0赞

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID’s.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

  • Line #1: the 7-digit ID number;
  • Line #2: the book title — a string of no more than 80 characters;
  • Line #3: the author — a string of no more than 80 characters;
  • Line #4: the key words — each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
  • Line #5: the publisher — a string of no more than 80 characters;
  • Line #6: the published year — a 4-digit number which is in the range [1000, 3000].

It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer M (<=1000) which is the number of user’s search queries. Then M lines follow, each in one of the formats shown below:

  • 1: a book title
  • 2: name of an author
  • 3: a key word
  • 4: name of a publisher
  • 5: a 4-digit number representing the year

Output Specification:

For each query, first print the original query in a line, then output the resulting book ID’s in increasing order, each occupying a line. If no book is found, print “Not Found” instead.

Sample Input:

  1. 3
  2. 1111111
  3. The Testing Book
  4. Yue Chen
  5. test code debug sort keywords
  6. ZUCS Print
  7. 2011
  8. 3333333
  9. Another Testing Book
  10. Yue Chen
  11. test code sort keywords
  12. ZUCS Print2
  13. 2012
  14. 2222222
  15. The Testing Book
  16. CYLL
  17. keywords debug book
  18. ZUCS Print2
  19. 2011
  20. 6
  21. 1: The Testing Book
  22. 2: Yue Chen
  23. 3: keywords
  24. 4: ZUCS Print
  25. 5: 2011
  26. 3: blablabla

Sample Output:

  1. 1: The Testing Book
  2. 1111111
  3. 2222222
  4. 2: Yue Chen
  5. 1111111
  6. 3333333
  7. 3: keywords
  8. 1111111
  9. 2222222
  10. 3333333
  11. 4: ZUCS Print
  12. 1111111
  13. 5: 2011
  14. 1111111
  15. 2222222
  16. 3: blablabla
  17. Not Found

题目大意:

数字图书馆中有数以百万计的书籍,根据书的书名、作者、他们的摘要、出版商和出版日期的关键字来储存。每一本书都有一个唯一的7位数字作为他的ID。给定读者的查询,你的任务是按照ID序列输出查询到的书籍。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含一个正整数N(<=10000),表示书的总数。然后接下来是N本书,每本书都包含6行信息:
第1行:7位数字的ID号;
第2行:书名—一个不超过80个字符的字符串;
第3行:作者—一个不超过80个字符的字符串;
第4行:关键字—每个单词都是不超过10个字符的字符串,没有空格,关键字只被一个空格隔开;
第5行:发布者—一个不超过80个字符的字符串;
第6行:出版日期—一个4位数,范围[1000,3000].
假设每本书只属于只属于一个作者,包含不超过5个关键字;总数不超过1000不同的关键字个;而且不超过1000家不同的出版商。
在图书信息之后,有一行包含一个正整数M(<=1000),这是用户搜索查询的数量。然后接下来是M行,每一行格式如下:
1:一个书名。
2:作者姓名。
3:一个关键字。
4:出版商的名字。
5:一个4位数字代表一年。
输出规范:
对于每个查询,首先在一行中打印出原始查询,然后按升序输出结果图书的ID,每个占用一行,如果没有找到书,打印“Not Found”。

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. struct node
  5. {
  6. char ID[10];
  7. char title[100];
  8. char author[100];
  9. char keywords[100];
  10. char publisher[100];
  11. char publishedyear[10];
  12. }libary[10001];
  13. int cmp(const void *a,const void *b)
  14. {
  15. return strcmp((*(node *)a).ID,(*(node *)b).ID)>0?1:-1;
  16. }
  17. int main()
  18. {
  19. int i,j,n,m,k,t,flag;
  20. char str[100],c;
  21. scanf("%d",&n);
  22. getchar();
  23. for(i=0;i<n;i++)
  24. {
  25. gets(libary[i].ID);
  26. gets(libary[i].title);
  27. gets(libary[i].author);
  28. gets(libary[i].keywords);
  29. gets(libary[i].publisher);
  30. gets(libary[i].publishedyear);
  31. }
  32. qsort(libary,n,sizeof(libary[0]),cmp);
  33. scanf("%d",&m);
  34. for(i=0;i<m;i++)
  35. {
  36. scanf("%d: ",&k);
  37. gets(str);
  38. printf("%d: %s\n",k,str);
  39. flag=0;
  40. for(j=0;j<n;j++)
  41. {
  42. if(strcmp(str,libary[j].title)==0)
  43. {
  44. printf("%s\n",libary[j].ID);
  45. flag=1;
  46. }
  47. else if(strcmp(str,libary[j].author)==0)
  48. {
  49. printf("%s\n",libary[j].ID);
  50. flag=1;
  51. }
  52. else if(strcmp(str,libary[j].publisher)==0)
  53. {
  54. printf("%s\n",libary[j].ID);
  55. flag=1;
  56. }
  57. else if(strcmp(str,libary[j].publishedyear)==0)
  58. {
  59. printf("%s\n",libary[j].ID);
  60. flag=1;
  61. }
  62. else if(strstr(libary[j].keywords,str)!=NULL)
  63. {
  64. printf("%s\n",libary[j].ID);
  65. flag=1;
  66. }
  67. }
  68. if(flag==0)
  69. {
  70. printf("Not Found\n");
  71. }
  72. }
  73. return 0;
  74. }

错误代码:

感觉这样做应该可以啊,不知道哪错了,求大神告知。。。。。。

70

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. struct node
  5. {
  6. char ID[10];
  7. char title[100];
  8. char author[100];
  9. char keywords[100];
  10. char publisher[100];
  11. int publishedyear;
  12. }libary[10001];
  13. int cmp(const void *a,const void *b)
  14. {
  15. return strcmp((*(node *)a).ID,(*(node *)b).ID)>0?1:-1;
  16. }
  17. int main()
  18. {
  19. int i,j,n,m,k,t,flag;
  20. char str[100],c;
  21. scanf("%d",&n);
  22. for(i=0;i<n;i++)
  23. {
  24. getchar();
  25. gets(libary[i].ID);
  26. gets(libary[i].title);
  27. gets(libary[i].author);
  28. gets(libary[i].keywords);
  29. gets(libary[i].publisher);
  30. scanf("%d",&libary[i].publishedyear);
  31. }
  32. qsort(libary,n,sizeof(libary[0]),cmp);
  33. scanf("%d",&m);
  34. for(i=0;i<m;i++)
  35. {
  36. scanf("%d",&k);
  37. getchar();
  38. getchar();
  39. switch(k)
  40. {
  41. case 1:
  42. gets(str);
  43. flag=0;
  44. printf("%d: %s\n",k,str);
  45. for(j=0;j<n;j++)
  46. {
  47. if(strcmp(str,libary[j].title)==0)
  48. {
  49. printf("%s\n",libary[j].ID);
  50. flag=1;
  51. }
  52. }
  53. if(flag==0)
  54. printf("Not Found\n");
  55. break;
  56. case 2:
  57. gets(str);
  58. flag=0;
  59. printf("%d: %s\n",k,str);
  60. for(j=0;j<n;j++)
  61. {
  62. if(strcmp(str,libary[j].author)==0)
  63. {
  64. printf("%s\n",libary[j].ID);
  65. flag=1;
  66. }
  67. }
  68. if(flag==0)
  69. printf("Not Found\n");
  70. break;
  71. case 3:
  72. gets(str);
  73. flag=0;
  74. printf("%d: %s\n",k,str);
  75. for(j=0;j<n;j++)
  76. {
  77. if(strstr(libary[j].keywords,str)!=NULL)
  78. {
  79. printf("%s\n",libary[j].ID);
  80. flag=1;
  81. }
  82. }
  83. if(flag==0)
  84. printf("Not Found\n");
  85. break;
  86. case 4:
  87. gets(str);
  88. flag=0;
  89. printf("%d: %s\n",k,str);
  90. for(j=0;j<n;j++)
  91. {
  92. if(strcmp(str,libary[j].publisher)==0)
  93. {
  94. printf("%s\n",libary[j].ID);
  95. flag=1;
  96. }
  97. }
  98. if(flag==0)
  99. printf("Not Found\n");
  100. break;
  101. case 5:
  102. scanf("%d",&t);
  103. flag=0;
  104. printf("%d: %d\n",k,t);
  105. for(j=0;j<n;j++)
  106. {
  107. if(libary[j].publishedyear==t)
  108. {
  109. printf("%s\n",libary[j].ID);
  110. flag=1;
  111. }
  112. }
  113. if(flag==0)
  114. printf("Not Found\n");
  115. break;
  116. }
  117. }
  118. return 0;
  119. }

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Digits

    \\(Digits\\) ![m6C1AO.png][] 这道题目比较简单,首先先打出来暴力,然后一看\\(b\\)的范围,瞬间想到快速幂。 快速幂的精髓是什么?

    相关 B1022

    1022 D进制的A+B (20)(20 分) 输入两个非负10进制整数A和B(<=230\-1),输出A+B的D (1 < D <= 10)进制数。 输入格式: 输