HDU 1062 Text Reverse (模拟)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1062

代码:

滕学渣代码格式混乱版。

#include<stdio.h>
#include<string.h>
#include<stack>

using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    int flag=0;
    while(t--)
    {
        if(flag==0)
        {
            flag=1;
            getchar();
        }
        char a[1005];
        memset(a,0,sizeof(a));
        gets(a);
        stack<char>q;
        int j=0;
        while(j!=strlen(a)+1)
        {
            int i;
            for(i=j; i<strlen(a); i++)
            {
                if(a[i]!=' ')
                    q.push(a[i]);
                if(a[i]==' '||a[i]=='\0')
                    break;
            }
            j=i+1;
            while(!q.empty())
            {
                printf("%c",q.top());
                q.pop();
            }
            if(j==strlen(a)+1)
                break;
            printf(" ");
            //printf("%d %d\n",i,j);
        }
        //printf("\n");
        //if(t)
            printf("\n");
    }
}

宋学霸代码格式清晰版

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;

char a[1005];
int main()
{
    stack<char>s;
    int t;
    scanf("%d\n",&t);
    while(t--)
    {
        while(!s.empty())
        {
            s.pop();
        }
        gets(a);
        int l=strlen(a);
        for(int i=0;i<l;i++)
        {
            if(a[i]!=' ')
            {
                s.push(a[i]);
            }
            if(a[i]==' ')
            {
                while(!s.empty())
                {
                    printf("%c",s.top());
                    s.pop();
                }
                printf(" ");
            }
        }
        while(!s.empty())
        {
            printf("%c",s.top());
            s.pop();
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(HDU 1062 Text Reverse (模拟))