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.对于每个身份证号, ...
随机推荐
- 静默安装oracle 11g,环境预检查时报错,SEVERE: [FATAL] PRVF-0002 : 无法检索本地节点名
环境描述: 操作系统:Redhat 6.6_x64 oracle:11.2.0.4 x64 问题描述: 今天在安装oracle 11g的数据库,在进行预安装环境检查的时候,报下面的错误: [oracl ...
- CreateEvent和SetEvent及WaitForSingleObject的使用方法
CreateEvent: 1.函数功能: 创建一个命名或匿名的事件对象 2.函数原型: HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttri ...
- POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)
Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...
- laravel安装 redis 并驱动 session
1)composer 安装 redis composer require predis/predis 如果感兴趣,可以看一下这里 2)配置 redis 连接(config/database.php 配 ...
- ubuntu系统无eth0网卡解决办法
ubuntu终端下命令ifconfig的问题解决 问题一. ifconfig之后只显示lo,没有看到eth0 问题二. ifconfig之后显示eth0,但是没有显示静态IP地址,即无inet.地址. ...
- linux系统socket通信编程1
Linux下的Socket编程大体上包括Tcp Socket.Udp Socket即Raw Socket这三种,其中TCP和UDP方式的Socket编程用于编写应用层的socket程序,是我们用得比较 ...
- C语言之选择结构
该章内容:本章我们学习三大结构之一:选择结构,采用选择结构来解决问题称为判断问题,它的求解规则是在不同的条件下进行不同的操作.选择结构比顺序结构要复杂一些.本章是考试的重点章节. 学习方法:先了解选择 ...
- diff命令的参数详解和实例
diff命令参数: diff - 找出两个文件的不同点 总览 diff [选项] 源文件 目标文件 描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - ...
- <转>主成分分析(Principal components analysis)-最大方差解释,最小平方差解释
转自http://www.cnblogs.com/jerrylead/archive/2011/04/18/2020209.html http://www.cnblogs.com/jerrylead/ ...
- 我觉得epoll和select最大的区别
最近在用epoll,网速资料很多,大家都说epoll和select的区别比较大,而且select要不停遍历所有的fd,效率要低,而且fd有限制. 但是我认为二者最大的区别在于 先看代码 while ( ...