1031 Hello World for U (20 分)

1031 Hello World for U (20 分)

Given any string of N (≥) 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 n?1??characters, then left to right along the bottom line with n?2?? characters, and finally bottom-up along the vertical line with n?3?? characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n?1??=n?3??=max { k | k≤n?2?? for all 3 } with n?1??+n?2??+n?3??−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

模拟一下
 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4 string s;
 5
 6 int main(){
 7     cin >> s;
 8     int len = s.length() + 2;
 9     int an = len/3;
10     int ans = s.length() - an*2;
11     int left = 0, right = s.length() -1;
12     for(int i = 1; i < an; i++){
13         cout <<s[left++];
14         for(int j = 0; j < ans; j++)
15             cout <<" ";
16         cout <<s[right--]<<endl;
17     }
18     for(int i = left; i <= right; i++){
19         cout <<s[i];
20     }
21     cout << endl;
22     return 0;
23 }

原文地址:https://www.cnblogs.com/zllwxm123/p/11075085.html

时间: 2024-10-17 09:48:58

1031 Hello World for U (20 分)的相关文章

1031 Hello World for U (20分)

Given any string of N (≥) 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

【PAT甲级】1031 Hello World for U (20 分)

题意: 输入一个字符串长度为5~80,以'U'型输出,使得底端一行字符数量不小于侧面一列,左右两列长度相等. trick: 不把输出的数组全部赋值为空格为全部答案错误,可能不赋值数组里值为0,赋值后是' ',空格的ascii是32,初读题面时并没有看到要输出空格,因为打印0其实效果看起来好像一样... 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;string s;char a[87][87

PAT Advanced 1031 Hello World for U (20分)

Given any string of N (≥) 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

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,

PTA-C-4-7 统计某类完全平方数 (20分)

4-7 统计某类完全平方数   (20分) 本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144.676等. 函数接口定义: int IsTheNumber ( const int N ); 其中N是用户传入的参数.如果N满足条件,则该函数必须返回1,否则返回0. 裁判测试程序样例: #include <stdio.h> #include <math.h> int IsTheNumber ( const int N ); int ma

PTA 10-排序4 统计工龄 (20分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数NN(\le 10^5≤10?5??),即员工总人数:随后给出NN个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:"工龄:人数".每项占一行.如果人数为0则不输出该项. 输入样

PTA 邻接表存储图的广度优先遍历(20 分)

6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ); 其中LGraph是邻接表存储的图,定义如下: /* 邻接点的定义 */ typedef struct AdjVNode *PtrToAdjVNode; struct AdjVNode{ Vertex AdjV; /* 邻接点下标 */ PtrToAdjVNode Next; /*

6-1 单链表逆转(20 分)

6-1 单链表逆转(20 分) 本题要求实现一个函数,将给定的单链表逆转. 函数接口定义: List Reverse( List L ); 其中List结构定义如下: typedef struct Node *PtrToNode; struct Node { ElementType Data; /* 存储结点数据 */ PtrToNode Next; /* 指向下一个结点的指针 */ }; typedef PtrToNode List; /* 定义单链表类型 */ L是给定单链表,函数Rever

6-2 顺序表操作集(20 分)

6-2 顺序表操作集(20 分) 本题要求实现顺序表的操作集. 函数接口定义: List MakeEmpty(); Position Find( List L, ElementType X ); bool Insert( List L, ElementType X, Position P ); bool Delete( List L, Position P ); 其中List结构定义如下: typedef int Position; typedef struct LNode *List; str