码迷,mamicode.com
首页 > 其他好文 > 详细

【转载+部分正确】字符串-07. 说反话-加强版 (20)

时间:2015-02-11 15:58:00      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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

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

输出格式:每个测试用例的输出占一行,输出倒序后的句子,并且保证单词间只有1个空格。

输入样例:

Hello World   Here I Come

输出样例:

Come I Here World Hello

 

#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
    stack<string> s;
    string  sp;
    bool flag=false;
    while(cin>>sp)
        s.push(sp);
    
    while(!s.empty())
    {
        if(flag)
            cout<<" ";
        else
            flag=true;
        cout<<s.top();
        s.pop();
    
    }
    return 0;
}

  

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <string>
#include <math.h>


using namespace::std; 

int main(){
  
    char mm[1000][1000];
    char s[1000];
   
    gets(s);
    
    memset(mm,\0,sizeof(mm));
    int cont = 0, k = 0;
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
      if(s[i] ==  &&s[i+1]!= &&s[i+1]!=\0)
      {
        cont++;
        k = 0;
      }else if(s[i] ==  &&(s[i+1]== ||s[i+1]==\0))
      {
          
      }
      else
        mm[cont][k++]=s[i];
    }
    printf("%s",mm[cont]);
    for(int i = cont-1; i >= 0; i--)
    {
      printf(" %s",mm[i]);
    }
    printf("\n");
   
      return 0;
}

 

【转载+部分正确】字符串-07. 说反话-加强版 (20)

标签:

原文地址:http://www.cnblogs.com/ligen/p/4286196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!