zoukankan      html  css  js  c++  java
  • google在线测试练习2

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


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    PID控制心得 2013/2/11
    在LaTeX文档中插入图片的几种常用的方法
    学习总结 2013/2/11
    eclipse 中引用其他项目及项目打包
    随笔2013/2/13
    随笔2013/2/19
    【转载】Latex对中文的支持 模版
    Latex 第二个程序
    Fences 桌面图标整理收纳箱
    消除“星期一综合症” 大前研一的周末时间分配术
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4887991.html
Copyright © 2011-2022 走看看