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 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≤n?2??≤N } 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

分析:图形输出题,意思是输出一个U的形状,不难发现规律,n1=n3=(N+2)/3向下取整,而n2=N+2-n1-n3,代码如下
 1 /**
 2 * Copyright(c)
 3 * All rights reserved.
 4 * Author : Mered1th
 5 * Date : 2019-02-23-23.40.16
 6 * Description : A1031
 7 */
 8 #include<cstdio>
 9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19
20 int main(){
21 #ifdef ONLINE_JUDGE
22 #else
23     freopen("1.txt", "r", stdin);
24 #endif
25     string str;
26     cin>>str;
27     int len=str.length();
28     int n1,n2,n3;
29     n1=(len+2)/3;
30     n3=n1;
31     n2=len+2-n1-n3;
32     int i;
33     for(i=0;i<n1-1;i++){
34         printf("%c",str[i]);
35         for(int j=0;j<n2-2;j++){
36             printf(" ");
37         }
38         printf("%c\n",str[len-i-1]);
39     }
40     for(i=0;i<n2;i++){
41         printf("%c",str[n1+i-1]);
42     }
43     return 0;
44 }

原文地址:https://www.cnblogs.com/Mered1th/p/10425010.html

时间: 2024-11-09 02:32:21

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

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, s

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

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