PAT (Advanced Level) Practice 1031 Hello World for U (20分)
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 | 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
好吧,来一个甲级中唯一要求输出字符串的20分题吧
观察输出图形,两竖一横,竖的字符串长度相同,如何确定横和竖的长度大小?
设n1、n2分别代表竖的长度,n1=n2,n3代表底层横的长度,按照题意n1+n2+n3=n+2,其中n为字符串长度
n3是三个数中最大的,且输出图形要尽可能看起来是正方形,什么意思?意思就是这三个数绝对值不超过1啊
一顿分析之后发现n1=n2=(n+2)/3,n3=n+2-n1-n2
之后就是很简单的去推一下哪个位置该输出字符串的哪个字符就好了~
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin>>s;
int k=s.size()+2;
int n1=k/3,n2=k/3,n3=k-n1-n2;
for(int i=1;i<n1;i++)
{
for(int j=1;j<=n3;j++)
{
if(j==1)cout<<s[i-1];
else if(j==n3) cout<<s[k-2-i];
else cout<<" ";
}cout<<endl;
}
for(int i=n1-1;i<k-n2-1;i++)cout<<s[i];
cout<<endl;
//while(1)getchar();
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法