HDU 1062.Text Reverse【栈或数组或字符串流】【字符处理】【8月30】

Text Reverse

Problem Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case contains a single line with several words. There will be at most 1000 characters in a line.

Output

For each test case, you should output the text which is processed.

Sample Input

3
olleh !dlrow
m‘I morf .udh
I ekil .mca

Sample Output

hello world!
I‘m from hdu.
I like acm.

Hint

Remember to use getchar() to read ‘\n‘ after the interger T, then you may use gets() to read a line and process it.

这个题可以用数组,也可以用栈,还可以用C字符串流来做。要注意,中间可能不止1个空格。坑!注意特殊的就可以了。用栈代码如下:

#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
int main(){
    int t;
    scanf("%d",&t);
    getchar();//吸收回车
    while(t--){
        char x;
        stack<char>f;
        while(1){
            x=getchar();
            if(x!=' '&&x!='\n'&&x!=EOF) f.push(x);
            else{
                while(!f.empty()){
                    printf("%c",f.top());
                    f.pop();
                }
                if(x=='\n'||x==EOF) break;
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}

用C字符串流代码如下:

#include<cstdio>
#include<sstream>
#include<iostream>
using namespace std;
int main(){
    int T;
    string s,ss;
    scanf("%d",&T);
    getchar();
    while(T--){
        getline(cin,s);
        int x=0;
        while(s[x]==' '){//开头可能有空格
            printf(" ");
            x++;
        }
        istringstream sin(s);
        while(sin>>ss){
            x+=ss.length();
            for(int i=ss.length()-1;i>-1;i--)
                printf("%c",ss[i]);
            while(x<s.length()&&s[x]==' '){//中间空格可能不是一个
                printf(" ");
                x++;
            }
        }
        printf("\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-17 14:41:52

HDU 1062.Text Reverse【栈或数组或字符串流】【字符处理】【8月30】的相关文章

HDU 1062 Text Reverse(水题,字符串处理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. 1 #include<cstdio> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath> 6 #include<deque> 7 #include&

hdu 1062 Text Reverse 字符串反转

Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18240    Accepted Submission(s): 6900 Problem Description Ignatius likes to write words in reverse way. Given a single line of text

HDOJ/HDU 1062 Text Reverse(字符串翻转~)

Problem Description Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them. Input The input contains several test cases. The first line of the inpu

题解报告:hdu 1062 Text Reverse

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题思路:问题求解的是单词的反转... AC代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 char a[1005]; 4 int main() 5 { 6 int T,len,x,y; 7 while(cin>>T){ 8 getchar();//吃掉回车符 9 while(T--){ 10 gets(a); 11

HDU 1062 Text Reverse

字符串反转:每个单词反转,然后输出. PE做法:所有单词反转并写入另一个数组中,附代码 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int N=1005; 6 int main() { 7 char ol[N],no[N]; 8 int n; 9 // freopen("C:\\CODE\\in.txt",

[hdu 1062] Text Reverse | STL-stack

原题 题目大意: t组数据,每组为一行,遇到空格时讲前面的单词反转输出. 题解: 显然的栈题,遇到空格时将当前栈输出清空即可 #include<cstdio> #include<stack> #include<cstring> using namespace std; int t,l; char s[1010]; stack <char> stk; int main() { scanf("%d",&t); getchar(); w

字节数组与字符串(字符数组)的转换操作

1.默认编码方式转换: (1)string(char[])转换为byte[] byte[] byteArr = System.Text.Encoding.Default.GetBytes(char[]); byte[] byteArr = System.Text.Encoding.Default.GetBytes(string); byte[] byteArr = System.Text.Encoding.Default.GetBytes(char[], startindex, count);

【HDOJ】1062 Text Reverse

#include<iostream>#include<string>using namespace std;void main(){ int T; while (cin>>T) { getchar(); for (int i = 0; i < T; i++) { string str; getline(cin, str); int end = 0,top = 0; while (str[end] != '\0') { if (str[end]==' ') { fo

黑马程序员——Java基础——IO流(三)—序列流、管道流、RandomAccessFile类、操作基本数据类型的流对象、操作数组和字符串、字符编码

第一讲 对象序列化(持久化) 一.概述:就是把对象封存在硬盘,可以保持数据:关键类:ObjectInputStream和ObjectOutpurStream 二. 关键字:ObjectOutputStream:方法有writerObject()读取 ObjectInputStream 方法有readObject() 被序列化的对象需要 implements Serializable关于被序列化的类需要实现Serializable它等于一个撮,标识用的,改变类里面的语句就变了.如果想固定一个撮,可