PAT1031 Hello World for U

Sample Input:

helloworld!

Sample Output:

h   !
e   d
l   l
lowor

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    string str;
    cin>>str;
    int length = str.length();
    int n1 = length/3;
    int n2;
    if (length%3 == 0)
    {
        n1--;
        n2 = n1 + 3;
    }
    else
    {
        n2 = n1 + length%3;
    }
    for (int i = 0; i < n1; i++)
    {
        cout<<str[i];
        for (int j = 0; j < n2 - 2; j++)
        {
            cout<<" ";
        }
        cout<<str[length - 1 -i]<<endl;
    }
    for (int i = length - n1 - n2; i < length - n1; i++)
    {
        cout<<str[i];
    }
    return 0;
}
 

猜你喜欢

转载自ppcool.iteye.com/blog/1733674
今日推荐