• 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;
            }
        }
    }

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

  • 相关阅读:
    三种renderman规范引擎的dice对比
    球形环境映射之angular与latlong格式互换
    SharePoint中的ASHX
    如何查看SharePoint未知错误的详细信息
    在SQL Server 2008设置发送邮件步骤详解
    项目管理软件对比
    海外云服务器VPS
    国内和国外域名注册商介绍
    快速将一个表的数据生成SQL插入语句
    使用sql server 链接服务器
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965308.html
Copyright © 2020-2023  润新知