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
  • 相关阅读:
    随手记录---transform 属性
    界面实例--图片布局在前端
    随手记录---jq如何判断当前元素是第几个元素
    PDF.Js的使用—javascript中前端显示pdf文件
    Jszip的使用和打包下载图片
    有关Canvas的一点小事—canvas和resize
    form input限制
    idea打war包正确姿势
    轻松建站神器!15个超精致的Bootstrap网站模板下载
    bootstrap教程
  • 原文地址:https://www.cnblogs.com/gpsx/p/5212738.html
Copyright © 2011-2022 走看看