zoukankan      html  css  js  c++  java
  • pat 1031. Hello World for U (20)

    1031. Hello World for U (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, "helloworld" can be printed as:

    h  d
    e  l
    l  r
    lowo
    
    That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

    Input Specification:

    Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.

    Output Specification:

    For each test case, print the input string in the shape of U as specified in the description.

    Sample Input:
    helloworld!
    
    Sample Output:
    h   !
    e   d
    l   l
    lowor
    
    解:这一题卡的时间挺长,有几个测试用例一直过不去,主要是题目的条件n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.这里没有注意。比如说hello,应该是
    h  o
    e l l
    还有N%3的情况需要特别注意下,我就是卡在这里,要保证n2>=n1=n3。
    贴上AC代码:
    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    using namespace std;
    //字符串长度为5的时候特殊考虑
    int main()
    {
        string s;
        while(cin>>s)
        {
           // cout<<s<<endl;
            int len=s.length();
            if(len==5)
            {
                cout<<s[0]<<" "<<s[4]<<endl;
                cout<<s[1]<<s[2]<<s[3]<<endl;
                continue;
            }
            if((len+2)%3==0)
            {
                for(int i=0;i<(len-1)/3;i++)
                {
                    cout<<s[i];
                    for(int i=0;i<(len-4)/3;i++)
                        cout<<' ';
                    cout<<s[len-i-1]<<endl;
                }
                for(int i=(len-1)/3;i<(2*len+1)/3;i++)
                {
                    cout<<s[i];
                }
                cout<<endl;
                continue;
            }
            if((len+1)%3==0)
            {
                for(int i=0;i<(len-2)/3;i++)
                {
                    cout<<s[i];
                    for(int i=0;i<(len-2)/3;i++)
                        cout<<' ';
                    cout<<s[len-i-1]<<endl;
                }
                for(int i=(len-2)/3;i<(2*len+2)/3;i++)
                {
                    cout<<s[i];
                }
                cout<<endl;
                continue;
            }
            if(len%3==0)
            {
                for(int i=0;i<(len)/3-1;i++)
                {
                    cout<<s[i];
                    for(int i=0;i<(len)/3;i++)
                        cout<<' ';
                    cout<<s[len-i-1]<<endl;
                }
                for(int i=(len)/3-1;i<(2*len)/3+1;i++)
                {
                    cout<<s[i];
                }
                cout<<endl;
                continue;
            }
        }
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    【BZOJ5306】染色(HAOI2018)-容斥原理+NTT
    【BZOJ3129】方程(SDOI2013)-容斥原理+扩展Lucas定理
    【BZOJ3876】支线剧情(AHOI&JSOI2014)-有上下界费用流
    【POJ1149】PIGS-最大流+优化建模
    【BZOJ1941】Hide and Seek(SDOI2010)-KD树
    【BZOJ1834】网络扩容(ZJOI2010)-最大流+费用流+拆边
    【BZOJ1927】星际竞速(SCOI2010)-费用流+拆点
    【BZOJ4872】分手是祝愿(六省联考2017)-期望DP
    【BZOJ2879】美食节(NOI2012)-费用流+拆点+动态加边
    JQ简单图片轮播
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965308.html
Copyright © 2011-2022 走看看