• 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
  • 相关阅读:
    Single Number II
    Pascal's Triangle
    Remove Duplicates from Sorted Array
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Unique Paths
    Sort Colors
    Swap Nodes in Pairs
    Merge Two Sorted Lists
    Climbing Stairs
  • 原文地址:https://www.cnblogs.com/gpsx/p/5212738.html
Copyright © 2020-2023  润新知