PAT 1031. Hello World for U

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;


int main()
{
    char str[100];
    int len,n1,n2;
    cin>>str;
    len = strlen(str);

    //先求出n1和n2
    if( (len-1)%2 == 0 )
    {
        n1 = (len-1)/2;
        n2 = 3;
    }
    else
    {
        n1 = (len-2)/2;
        n2 = 4;
    }
    while( n1>n2 )
    {
        n1--;
        n2 += 2;
    }


    int b=0,e=len-1,i=0,j=0;
    for(i=0; i<n1; i++)
    {
        if(i==n1-1) //最后一行
        {
            for(j=0; j<n2; j++)
                cout<<str[b++];
            cout<<endl;
        }
        else
        {
            for(j=0; j<n2; j++)
            {
                if(j==0)
                    cout<<str[b++];
                else if(j==n2-1)
                    cout<<str[e--];
                else
                    cout<<" ";
            }
            cout<<endl;
        }

    }
    return 0;
}
posted @ 2012-12-01 15:39  Frank@609  Views(189)  Comments(0Edit  收藏  举报