google在线测试练习题2
来源:互联网 发布:山东大学网络教育平台 编辑:程序博客网 时间:2024/04/30 17:49
Problem
Given a list of space separated words, reverse the order of the words. Each line of text contains L
letters and W
words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.
Input
The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line.
Output
For each test case, output one line containing "Case #x: " followed by the list of words in reverse order.
Limits
Small dataset
N = 5
1 ≤ L ≤ 25
Large dataset
N = 100
1 ≤ L ≤ 1000
思路:先一个一个单词反转,再将整个字符串反转。
#include<iostream>#include<fstream>#include<string>using namespace std;int main(){int n_case;ifstream ifile("B-large-practice.in");ofstream ofile("resultb2.txt");ifile >> n_case;for(int i = 0; i <= n_case; i++){char line[1002];ifile.getline(line, 1002);string words(line);int begin = 0;int end = 0;if(i == 0)continue;for(int i = 1; i < words.length(); i++){if(words[i] == ' '){int sum = begin + i - 1;for(int j = begin; j <= sum / 2; j++){char tmp = words[j];words[j] = words[sum - j];words[sum - j] = tmp;}begin = i + 1;}}int sum = begin + words.length() - 1;for(int j = begin; j <= sum / 2; j++){char tmp = words[j];words[j] = words[sum - j];words[sum - j] = tmp;}sum = words.length() - 1;for(int j = 0; j <= sum / 2; j++){char tmp = words[j];words[j] = words[sum - j];words[sum - j] = tmp;}ofile << "Case #" << i << ": " << words << endl;}return 0;}
- google在线测试练习题2
- google在线测试练习题1
- google在线测试练习题3
- 测试一下GOOGLE 的在线相册
- 2014 google (在线)测试笔试题解
- google校招在线测试题---2048
- Google Kickstart在线测试规则以及注意事项
- 测试练习题
- scala在线练习题
- 2014校招 Google在线测试第二场题解 A
- 2014校招 Google在线测试第二场题解 B
- 2014校招 Google在线测试第二场题解 C
- 2014校招 Google在线测试第二场题解 D
- 2014校招 Google在线测试第二场题解 E
- Google Code Jam在线测试题目--Alien Language
- 2014校招 Google在线测试第二场题解 C
- 华为机试在线练习题
- 软件测试练习题
- 谁也无法预知一夜可以长于三十年老于三十年
- 玄宗裹了裹无以取暖的衾被
- 溃烂中的代码
- 晓康和几个友人在一家餐馆与我聚餐
- [入门]Ruby on Rails入门教程及开发工具选用
- google在线测试练习题2
- 黑马程序员:Java基础——多线程的停止与守护以及Join,优先级和yield方法
- 安卓事件处理
- CSDN CODE 使用笔记
- Netty源码解读(四)Netty与Reactor模式
- 使用autotools系列工具自动部署源代码编译安装
- 《疯狂Java讲义》笔记
- google在线测试练习题3
- 解说同源策略和跨域访问