String Processing - Algorithms notes

PAT 1009 ironic (20 points)

Given a word of English, asking you to write a program, all the words of the sentence order reversed output.

Input formats:

Test input comprises a test case, given the string length does not exceed a total of 80 in a row. String composed of several words and a number of spaces, where the word is English letters (case is case) consisting of string, separated by a space between words, the input end of the sentence to ensure that no extra space.

Output formats:

Each test case output per line, output sentence after the reverse.

Sample input:

Hello World Here I Come

 

Sample output:

Come I Here World Hello

answer:

//方法一
#include<iostream>
#include<stack> 
using namespace std;

int main(){
	stack<string> v;
	string s;
	while(cin>>s) v.push(s);
	cout<<v.top();
	v.pop();
	while(!v.empty()){
		cout<<" "<<v.top();
		v.pop();
	}
	return 0;
}

//方法二
#include<cstdio>

int main(){
    int num=0;
    char ans[90][90];
    while(scanf("%s",ans[num])!=EOF){
        num++;
    }
    for(int i = num-1;i>=0;i--){
        printf("%s",ans[i]);
        if(i>0) printf(" ");
    }
	return 0;
}

 

Published 98 original articles · won praise 2 · Views 3702

Guess you like

Origin blog.csdn.net/qq_30377869/article/details/105012572