Moist (cin,getline的区别)
来源:互联网 发布:上海盘石软件电话 编辑:程序博客网 时间:2024/06/06 00:27
Problem
Moist has a hobby – collecting figure skating trading cards. His card collection has been growing, and it is now too large to keep in one disorganized pile. Moist needs to sort the cards in alphabetical order, so that he can find the cards that he wants on short notice whenever it is necessary.
The problem is – Moist can’t actually pick up the cards because they keep sliding out his hands, and the sweat causes permanent damage. Some of the cards are rather expensive, mind you. To facilitate the sorting, Moist has convinced Dr. Horrible to build him a sorting robot. However, in his rather horrible style, Dr. Horrible has decided to make the sorting robot charge Moist a fee of $1 whenever it has to move a trading card during the sorting process.
Moist has figured out that the robot’s sorting mechanism is very primitive. It scans the deck of cards from top to bottom. Whenever it finds a card that is lexicographically smaller than the previous card, it moves that card to its correct place in the stack above. This operation costs $1, and the robot resumes scanning down towards the bottom of the deck, moving cards one by one until the entire deck is sorted in lexicographical order from top to bottom.
As wet luck would have it, Moist is almost broke, but keeping his trading cards in order is the only remaining joy in his miserable life. He needs to know how much it would cost him to use the robot to sort his deck of cards.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each one starts with a line containing a single integer, N. The next N lines each contain the name of a figure skater, in order from the top of the deck to the bottom.
Output
For each test case, output one line containing “Case #x: y”, where x is the case number (starting from 1) and y is the number of dollars it would cost Moist to use the robot to sort his deck of trading cards.
Limits
1 ≤ T ≤ 100.
Each name will consist of only letters and the space character.
Each name will contain at most 100 characters.
No name with start or end with a space.
No name will appear more than once in the same test case.
Lexicographically, the space character comes first, then come the upper case letters, then the lower case letters.
Small dataset
1 ≤ N ≤ 10.
Large dataset
1 ≤ N ≤ 100.
Sample:
Input
2
2
Oksana Baiul
Michelle Kwan
3
Elvis Stojko
Evgeni Plushenko
Kristi Yamaguchi
Output:
Case #1: 1
Case #2: 0
Solution
该题的排序方式是:从牌顶开始,如果当前牌比前一张牌小,则将当前牌移动到该牌所在的位置,移动一次价格加1.因此,每次只需要记录已排序好的最大牌,对下一张牌与最大牌进行比较,如果该牌比最大牌小,则需要移动,cost加1,否则,不需移动,将该牌的值赋给最大牌。
Source Code
特别需要注意的是:
cin读取以空格“ “为分界,当读到换行符’\n’时,会将换行符留在输入流里。下一次读取如果用cin.getline时,会先将上一行的换行符读取,而不会得到期望的下一行结果。getline读取时将输入流中的换行符也一并读入。
char *a={“012345”};
strlen(a)读的是a的实际长度,不包含\n,所以为6.
#include <iostream>using namespace std;int main(void){ freopen("test.in", "r", stdin); freopen("result.out", "w", stdout); int count, i; //string max, t1, t2; char max[100], t1[100]; cin >> count; char n[100]; for (i = 1; i <= count; i++) { int num; cin >> num;//cin时将换行符\n留在了输入流里,所以下一个getline它会先读上一行的\n. int cost = 0; cin.getline(t1, 100);//读取cin留下的换行符 for (int j = 0; j < num; j++) { cin.getline(t1, 100); if (j == 0) strcpy(max, t1); if (strcmp(max, t1) > 0)//max 比当前元素大,需要移动 cost++; else//max小于当前元素,不需移动 strcpy(max, t1); } cout << "Case #" << i << ": " << cost << endl; }}
- Moist (cin,getline的区别)
- cin.get(),cin,cin.getline()的区别
- cin,cin.get,cin.getline的区别
- cin,cin.get,cin.getline,getline的区别
- getline和cin.getline的区别!
- cin.getline、getline和gets的区别
- getline()和cin.getline()的区别
- getline和cin.getline的区别
- cin.getline()和getline()的区别联系
- cin.get和cin.getline的区别?
- cin.get和cin.getline的区别?
- cin.get和cin.getline的区别
- cin.get()与cin.getline()的区别
- cin.get()与cin.getline()的区别
- cin.get()与cin.getline()的区别
- cin与cin.getline()的区别
- cin.get( )与cin.getline( )的区别
- cin.getline()和cin.get() 的区别
- 字符串处理函数:复值函数strcpy、比较函数strcmp、连接函数strcat、大写转换函数strupr
- 杭电 1869 六度分离(floyd &&dijkstra)
- iOS 设计模式之工厂模式
- 关于android中控件位置属性的解释
- 1090. Highest Price in Supply Chain (25)
- Moist (cin,getline的区别)
- Python StringIO与cStringIO
- JAVA_SE基础——23.类的定义
- hdu5102 The K-th Distance bfs+结构体
- 时间序列学习笔记
- MyEclipse配置Hibernate/MyEclipse反向工程从数据表生成POJO和对象关系映射文件(以及HibernateSessionFactory)
- ACM经典算法之数学问题模板
- SQL注入之DVWA平台测试mysql注入
- Unity Asset(3)虚拟摇杆插件 EasyTouch