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 n1characters, 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
题目大意:给出一个字符串,要求按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/;
n2=n13+size%;
if(n13==n2){//如果两者相等的话,那么需要进行修改。
n13-=;
n2=size-n13*;
} for(int i=;i<size;i++){
if(i<n13){
cout<<s[i];
for(int j=;j<n2-;j++){
cout<<' ';
}
cout<<s[size--i]<<'\n';
}else{
break;
} }
//打印n2层
for(int i=n13;i<size-n13;i++){
cout<<s[i];
}
return ;
}
1.第一次提交时没通过,因为是如果是n13和n2相等的话,中间的空格就没办法输出了,所以就又添加了一个if判断是否相等,如果相等的话,那么就进行修改,便通过了。
PAT 1031 Hello World for U[一般]的更多相关文章
- 浙大pat 1031题解
1031. Hello World for U (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Giv ...
- PAT 1031 Hello World for U
1031 Hello World for U (20 分) Given any string of N (≥) characters, you are asked to form the char ...
- PAT 1031 查验身份证(15)(C++&Python)
1031 查验身份证(15)(15 分) 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8, ...
- PAT——1031. 查验身份证
一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...
- PAT 1031. 查验身份证(15)
一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...
- PAT 1031 查验身份证
https://pintia.cn/problem-sets/994805260223102976/problems/994805290334011392 一个合法的身份证号码由17位地区.日期编号和 ...
- 1031. Hello World for U (20) PAT
题目:http://pat.zju.edu.cn/contests/pat-a-practise/1031 分析: 简单题,调整数组的输出次序就可以了. 输入数组长度为len.n1 = n3 = (l ...
- PAT (Basic Level) Practise (中文)-1031. 查验身份证(15)
PAT (Basic Level) Practise (中文)-1031. 查验身份证(15) http://www.patest.cn/contests/pat-b-practise/1031 一个 ...
- PAT乙级1031
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805290334011392 题解 emmm.对于每个身份证号, ...
随机推荐
- android 应用能够安装在什么地方
眼下我们仅仅能做到将应用安装在例如以下组合中: 1.关闭MTK_2SDCARD_SWAP,能够将应用安装在以下两个路径上 1)手机内存(internal storage) 2) ...
- swift开发之--报错:Class "***ViewController" has no initializers
因为Swift中要求变量或常量在声明时就要初始化其值,所以我们在实际开发中,声明变量或常量时使用可选类型. ?!1234 var stitle : UILabel?var webview : UIWe ...
- swift - 利用UIDatePicker实现定时器的效果
效果图如下: 可以通过UIDatePicker调整倒计时的时间,然后点击UIButton开始倒计时,使用NSTimer进行倒计时的时间展示,我是声明了一个label也进行了标记, 然后点击按钮开始倒计 ...
- 元素设置disabled属性后便无法向后台传值
元素设置disabled属性后便无法向后台传值
- Win 10安装Python及环境变量配置
一.Windows系统 很多童鞋问之前的教程怎么没有介绍安装python3.5的,现予以补充更新一下. (一)安装python3.5 1.下载 进入Python官网www.python.org,在“D ...
- 在centos命令行下安装软件
考虑到用linux纯命令行模式时,通常都是用作服务器,所以,一般主要是安装一个php.mysql.apache的集成环境和ftp服务器,在这里用xampp集成包即可解决所有问题,在这里说一下我自己的做 ...
- linux 学习的一些书单,对了解android 也有大用
要推荐的书,我在<那两年炼就的Android内功修养>这篇文章中有提到,这里再列一下出来: 语言类: <深度探索C++对象模型>,对应的英文版是<Inside C+++ ...
- LeetCode——Happy Number
Description: Write an algorithm to determine if a number is "happy". A happy number is a n ...
- Python汉英/英汉翻译(百度API/有道API)
一.百度API实现 Step1:申请API Key 以前用过BAE,已经有了Api Key,没有的可以去申请 Step2:挺简单,直接看实现的代码吧 ```python #coding:utf-8 i ...
- iPad - 开发(Universal Applications)
一.iPad 1.判断是否在iPad上 BOOL iPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdi ...