zoukankan      html  css  js  c++  java
  • PAT 1031 Hello World for U[一般]

    1031 Hello World for U (20 分)

    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 | kn2​​ for all 3n2​​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

     题目大意:给出一个字符串,要求按U型输出n1,n3分别是左边和右边从第一个到最后一个的高度,n2是底层的长度,要求满足n1=n3(k|k<=n2),也就是列的高度要小于等于底部的长度。

     //猛一看这个题目还挺难的,我的AC:

    #include <iostream>
    #include <vector>
    #include<cstdio>
    using namespace std;
    
    int main() {
        string s;
        cin>>s;
        int size=s.size();
        int n13,n2;
        n13=size/3;
        n2=n13+size%3;
        if(n13==n2){//如果两者相等的话,那么需要进行修改。
            n13-=1;
            n2=size-n13*2;
        }
    
        for(int i=0;i<size;i++){
            if(i<n13){
                cout<<s[i];
                for(int j=0;j<n2-2;j++){
                    cout<<' ';
                }
                cout<<s[size-1-i]<<'
    ';
            }else{
                break;
            }
    
        }
        //打印n2层
        for(int i=n13;i<size-n13;i++){
            cout<<s[i];
        }
        return 0;
    }

    1.第一次提交时没通过,因为是如果是n13和n2相等的话,中间的空格就没办法输出了,所以就又添加了一个if判断是否相等,如果相等的话,那么就进行修改,便通过了。

  • 相关阅读:
    HMM 学习一
    matlab buffer的使用
    JDK环境变量配置及Tomcat安装服务
    Js参数RSA加密传输,jsencrypt.js的使用
    Dapper基本增删改查
    Dapper事务操作
    Ubuntu知识记录
    InstallShield 覆盖安装
    Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
    Sql Server导出表结构Excel
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9984196.html
Copyright © 2011-2022 走看看