uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!
I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves, and creators of odd volumes. --Charles Lamb, Essays of Elia (1823) 'The Two Races of Men'
Like Mr. Lamb, librarians have their problems with borrowers too. People don't put books back where they should. Instead, returned books are kept at the main desk until a librarian is free to replace them in the right places on the shelves. Even for librarians, putting the right book in the right place can be very time-consuming. But since many libraries are now computerized, you can write a program to help.
When a borrower takes out or returns a book, the computer keeps a record of the title. Periodically, the librarians will ask your program for a list of books that have been returned so the books can be returned to their correct places on the shelves. Before they are returned to the shelves, the returned books are sorted by author and then title using the ASCII collating sequence. Your program should output the list of returned books in the same order as they should appear on the shelves. For each book, your program should tell the librarian which book (including those previously shelved) is already on the shelf before which the returned book should go.
Input
Output
Sample Input
"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
RETURN "Algorithms"
RETURN "The C Programming Language"
SHELVE
END
Sample Output
Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END 真是一看到各种麻烦的字符串的输入输出就不敢做了,真的对字符串好不熟悉。
这个题感觉数据的处理确实比较麻烦
以下为摘来的代码
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
using namespace std; struct book{
string title, author;
bool borrowed, returned;
book(string a,string b){
title = a;
author = b;
borrowed = returned = false;
}
}; vector<book> allbook;
string command,request,ss; bool cmpa(book a,book b)
{
return (a.author < b.author);
} bool cmpt(book a,book b)
{
return (a.title < b.title);
} void shelve()
{
int j;
for(int i = ; i < allbook.size();i++){
if(allbook[i].returned == true){
for(j = i; j >= ;j--){
if(allbook[j].borrowed == false)break;
}
if(j == -)printf("Put %s first\n", allbook[i].title.c_str()); //之前的所有书都被借走了
else printf("Put %s after %s\n",allbook[i].title.c_str(), allbook[j].title.c_str());
allbook[i].borrowed = allbook[i].returned = false;
}
}
cout << "END\n";
} void borrow()
{
getline(cin, request);
for(int i = ;i < allbook.size(); i++){
if(allbook[i].title == request){
allbook[i].borrowed = true;
return;
}
}
} void back()
{
getline(cin, request);
for(int i = ; i < allbook.size(); i++ ) {
if(allbook[i].title == request){
allbook[i].returned = true;
return;
}
}
} int main()
{
while(getline(cin,ss)&&ss!="END"){
allbook.push_back(book(ss.substr( , ss.find_last_of( "\"" ) + ),
ss.substr(ss.find_last_of( "\"" ) + )));
}
stable_sort(allbook.begin(), allbook.end(), cmpt);
stable_sort(allbook.begin(), allbook.end(), cmpa);
while(cin >> command){
if( command == "BORROW" )
cin.get(), borrow();
else if( command == "RETURN" )
cin.get(), back();
else if( command == "SHELVE" )
cin.get(), shelve();
}
//system("pause");
return ;
}
把C++中的string 转化为C中的%s输出,要用c_str() ,否则输出的是一堆乱码
看完后觉得操作并不是很难,然而思路和对字符串的处理都值得膜拜!
uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!的更多相关文章
- Uva - 230 - Borrowers
AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype ...
- UVa 230 Borrowers(map和set)
I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shelve ...
- UVA 230 Borrowers (STL 行读入的处理 重载小于号)
题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...
- Borrowers UVA - 230
I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shel ...
- C++重载流运算符,将存储结构体的vector直接写入文件
我们知道,当vector很大的时候,如果使用循环的方式将其中的元素写入文件将非常费时,因此有没有办法将vector一次性写入文件呢? 采用流运算符重载的方法可以做到,不仅基本类型的vector可以一次 ...
- 转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在 ...
- 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...
- C++中的结构体vector排序
在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了: // sort algorithm example #include ...
- TCP/IP协议头部结构体(网摘小结)(转)
源:TCP/IP协议头部结构体(网摘小结) TCP/IP协议头部结构体(转) 网络协议结构体定义 // i386 is little_endian. #ifndef LITTLE_ENDIAN #de ...
随机推荐
- webrtc之视频捕获模块--video_capture
webrtc的video_capture模块,为我们在不同端设备上采集视频提供了一个跨平台封装的视频采集功能,如下图中的webrtc的video_capture源码,现webrtc的video_cap ...
- js页面加载进度条(这个就比较正式了,改改时间就完事儿)
不废话,直接上代码 思路不难,就是一个animate方法配合随机数 duration内个三秒钟,是自定义的,可以改成页面加载时间,这样就完美了 <!doctype html> <ht ...
- Lua 字符串 匹配模式 总结
字符类 %a --字母alpha %d --数字double %l --小写字母lower %u --大写字母upper %w --字母和数字word %x -- 十六进制 %z --代表0 zero ...
- CC2530红外学习球学码函数(P1.2接红外一体接收头,使用定时器tim1的复用功能2)
P1.2GPIO配置: void cap_gpio_init(){ P1SEL |= 0x04; P1DIR &= ~0x04; PERCFG |= 0x40; P2SEL |= 0x20; ...
- My Eclipse 自动提示
1.My Eclipse 自带代码提示快捷键 “ alt+/”. 2.输入即提示:window-->preferences-->java-->Editor 展开后点击Content ...
- Effective Java2读书笔记-对于所有对象都通用的方法(二)
第10条:始终要覆盖toString 这一条没什么好讲的,就是说默认的toString方法打印出来的是类名+@+十六进制哈希码的值.我们应该覆盖它,使它能够展示出一些更为详细清晰的信息,这个看实际情况 ...
- 在Android模拟器中经常出现以下错误 timeout Launch canceled!
Failed to install MainActivity.apk on device 'emulator-5554': timeoutLaunch canceled! 解决方法: window-& ...
- ANSIC程序到KeilC51的移植心得
摘要:本文讲述了将ANSIC程序移植到KeilC51上应该注意的事项.文章讲述了存储类型.指针类型.重入函数.根据目标系统RAM的分布的段定位和仿真栈设置.函数指针.NULL指针问题.字节顺序.交叉汇 ...
- Android的init过程详解(一)(转)
本文使用的软件版本 Android:4.2.2 Linux内核:3.1.10 本文及后续几篇文章将对Android的初始化(init)过程进行详细地.剥丝抽茧式地分析,并且在其中穿插了大量的知识,希望 ...
- 【转】Android兼容性测试CTS --环境搭建、测试执行、结果分析
原文网址:http://www.cnblogs.com/zh-ya-jing/p/4396918.html 为了确保Android应用能够在所有兼容Android的设备上正确运行,并且保持相似的用户体 ...