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.对于每个身份证号, ...
随机推荐
- swift--动画效果
一.for循环创建4*4个view,然后对立面的所有view进行动画,这里列集中动画的效果: 1,旋转动画 for tile in backgrounds{ //现将数字块大小职位原始尺寸的1/10 ...
- laravel 5.3 多用户认证
不知道对不对,乱来一下!!!! 1)laravel自带了一个用户认证系统,要使用的话,直接运行一下命令就可以用了 php artisan make:auth 会生成相应的控制器.路由和模版文件 2)数 ...
- 多线程模块:threading
threading 常见用法: (1) 在 thread 中,我们是通过 thread.start_new_thread(function, args) 来开启一个线程,接收一个函数和该函数的参数,函 ...
- 使用reduce的方法实现对象数组去重
在开发中和面试当中,数组去重问题往往是受宠儿,那用最短的代码解决这个问题会使效率得到更大的提升.普通的数组,我们可以通过filter过滤方法进行去重,详情见本人博客:http://www.cnblog ...
- php学习七:时间和日期
在学习php中的时间和日期的时候,必须要了解什么是时间戳,那么什么是时间戳呢,请看一下的定义 时间戳:从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数 ...
- (一)微信小程序之模拟调用后台接口踩过的坑
如下图标记的三个点 在调试过程中出现问题,特此记录. 1. 之前在浏览器测试接口习惯省略 http:// ,是因为浏览器默认有一个检测,在你输入的网址前面加http://,如果有就不加. 然而在微信小 ...
- ajax返回值传给js全局变量
1. $.ajaxSetup({ async : false //设置ajax为同步方式,异步方式的话在赋值时数据还未提取出来 });var t = ""; var enginee ...
- Java的String详解
Java的String详解 博客分类: Java javaStringString详解常用方法 Java的String类在开发时经常都会被使用到,由此可见String的重要性.经过这次认真仔细的学习 ...
- linux 提示符>怎样退出
在linux(Red Hat)字符界面下,不小心输入了上漂号 ’ ,结果命令提示符变成了>,然后在q.exit.ctrl+c.ctrl+z都回不去了,不知道怎么回到#的命令提示符? 表示ct ...
- java基础---->Java中枚举的使用(一)
这里介绍一下java中关于枚举的使用. java中枚举的使用 一.枚举中可以定义方法 参照于TimeUnit的使用,TimeUnit.MILLISECONDS.sleep(1000); LoveUti ...