zoukankan      html  css  js  c++  java
  • PAT 乙级 1009 说反话 (20) C++版

    1009. 说反话 (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    8000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

    输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。

    输出格式:每个测试用例的输出占一行,输出倒序后的句子。

    输入样例:
    Hello World Here I Come
    
    输出样例:
    Come I Here World Hello
    

     思路:将所有单词倒着存储,正向输出,方法1:使用栈,正好满足先进后出的特性,方法2使用其它容器

    方法1

     1 // 1009_1.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include<iostream>
     6 #include<stack>
     7 #include<string>
     8 
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     char c;
    14     stack<string> s;
    15     string str;
    16 
    17     while (cin >> str)
    18     {
    19         s.push(str);
    20 
    21         if ((c = getchar()) == '
    ')//判断输入是否完毕
    22             break;
    23     }
    24 
    25     cout<<s.top();
    26     s.pop();
    27 
    28     while (!s.empty())
    29     {
    30         cout << " " << s.top();
    31         s.pop();
    32     }
    33 
    34     return 0;
    35 }

    方法2: 

     1 // 1009.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include<iostream>
     6 #include<string>
     7 #include<vector>
     8 
     9 using namespace std;
    10 
    11 int main()
    12 {
    13     char c;
    14     vector<string> v;
    15     string str;
    16     int j ;
    17 
    18     while (cin >> str)
    19     {
    20         v.insert(v.begin(),str);
    21 
    22         if((c = getchar())=='
    ')
    23             break;
    24     }
    25 
    26     vector<string>::iterator i = v.begin(), end = v.end();
    27 
    28     for (j = v.size(); j>0; j--, i++)
    29     {
    30         cout << *i;
    31 
    32         if (j != 1)
    33             cout << " ";
    34     }
    35 
    36     return 0;
    37 }
  • 相关阅读:
    python3 flask 文件下载服务器
    jquery cdn加速
    cherry 与sqlite
    cherry 与react.js
    cherrypy json 解析
    cherrypy cookies
    cherrypy 打印日志
    cherrypy pytest 覆盖,测试代码
    cherrypy ajax 请求
    cherrypy 访问css文件
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/7138046.html
Copyright © 2011-2022 走看看