zoukankan      html  css  js  c++  java
  • HDU 1062 Text Reverse(字符串)

    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 ' ' after the interger T, then you may use gets() to read a line and process it.
     
    Wrong Answer
    用sstream去做,感觉挺对的啊,却一直PE。
    另外HDU最近有点抽啊。
     
    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <vector>
    #define PI acos(-1.0)
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    #define msd memset(dp,0,sizeof(dp))
    using namespace std;
    //#define LOCAL
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        //freopen("out.txt","w",stdout);
    #endif // LOCAL
        ios::sync_with_stdio(false);
        int N;
        cin>>N;
        string t;
        getline(cin,t);
        while(N--)
        {
            string s;
            s.clear();
            getline(cin,s);
            stringstream ss(s);
            string sss;
            ss>>sss;
            while(1)
            {
                string::iterator it=sss.end()-1;
                for(; it!=sss.begin(); it--)
                {
                    printf("%c",*it);
                }
                printf("%c",*it);
                if(ss>>sss)
                printf(" ",*sss.begin());
                else
                break;
            }
            printf("
    ");
        }
        return 0;
    }
    View Code
    Answer
    遇到空格倒序输出刚保存的文字,结尾的时候也输出一次。
     
    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <vector>
    #define PI acos(-1.0)
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    #define msd memset(dp,0,sizeof(dp))
    using namespace std;
    //#define LOCAL
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        //freopen("out.txt","w",stdout);
    #endif // LOCAL
        //ios::sync_with_stdio(false);
        int N;
        cin>>N;
        getchar();
        while(N--)
        {
            char a[1001];
            char b[1001];
            gets(a);
            for(int i=0,as=strlen(a),cur=0;i<as;i++)
            {
                b[cur++]=a[i];
                if(a[i]==' ')
                {
                    for(int j=cur-2;j>=0;j--)
                        printf("%c",b[j]);
                    printf(" ");
                    cur=0;
                }
                if(i==as-1)
                {
                    for(int j=cur-1;j>=0;j--)
                        printf("%c",b[j]);
                    printf("
    ");
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    08 linux文件检索和编辑
    Mybatis3详解(二十)——Mybatis中使用的9种设计模式(转)
    Mybatis3详解(十八)——Mybatis运行原理之Mapper接口的动态代理过程
    Mybatis3详解(十七)——Mybatis运行原理之SqlSession的构建过程
    Mybatis3详解(十六)——Mybatis运行原理之SqlSessionFactory的构建过程
    Mybatis3详解(十五)——Mybatis整合Spring框架
    Mybatis3详解(十四)——Mybatis的分页
    Mybatis3详解(十三)——Mybatis逆向工程
    Mybatis3详解(十二)——Mybatis缓存
    Mybatis3详解(十一)——延迟加载
  • 原文地址:https://www.cnblogs.com/gpsx/p/5212738.html
Copyright © 2011-2022 走看看