google在线测试练习题2

21 篇文章 0 订阅

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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值