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
  • 相关阅读:
    HTML 嵌入 SWF ,访问域的问题
    Flex 4 中,StageScaleMode 设置
    一句话清除MC下所有的子
    DataGrid 表头标题,表格文字的字体设置
    Flash中,将某个元件置于最顶层
    Android开发中使用SQLite 数据库
    Android多分辨率支持说明
    Android adb shell 命令大全
    Android Camera 使用小结
    Android多分辨率支持以及各种类型图标尺寸大小
  • 原文地址:https://www.cnblogs.com/gpsx/p/5212738.html
Copyright © 2011-2022 走看看