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 n1 characters, 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是底部横线从左到右的字符个数。
要求:
1. n1 == n3
2. n2 >= n1
3. n1为在满足上述条件的情况下的最大值

分析:假设n = 字符串长度 + 2,因为2 * n1 + n2 = n,且要保证n2 >= n1, n1尽可能地大,分类讨论:

1. 如果n % 3 == 0,n正好被3整除,直接n1 == n2 == n3;

2. 如果n % 3 == 1,因为n2要比n1大,所以把多出来的那1个给n2

3. 如果n % 3 == 2, 就把多出来的那2个给n2

所以得到公式:n1 = n / 3,n2 = n / 3 + n % 3

把它们存储到二维字符数组中,一开始初始化字符数组为空格,然后按照u型填充进去,最后输出这个数组u。

#include <iostream>
#include <string.h>
using namespace std;
int main() {char c[81], u[30][30];memset(u, ' ', sizeof(u));scanf("%s", c);int n = strlen(c) + 2;int n1 = n / 3, n2 = n / 3 + n % 3, index = 0;for(int i = 0; i < n1; i++) u[i][0] = c[index++];for(int i = 1; i <= n2 - 2; i++) u[n1-1][i] = c[index++];for(int i = n1 - 1; i >= 0; i--) u[i][n2-1] = c[index++];for(int i = 0; i < n1; i++) {for(int j = 0; j < n2; j++) printf("%c", u[i][j]);printf("\n");}return 0;
}

1031. Hello World for U (20)-PAT甲级真题相关推荐

  1. 1120. Friend Numbers (20)-PAT甲级真题

    1120. Friend Numbers (20) Two integers are called "friend numbers" if they share the same ...

  2. 1042. Shuffling Machine (20)-PAT甲级真题

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  3. 1081. Rational Sum (20)-PAT甲级真题

    Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...

  4. 1077. Kuchiguse (20)-PAT甲级真题

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  5. 1061. Dating (20)-PAT甲级真题

    Sherlock Holmes received a note with some strange strings: "Let's date! 3485djDkxh4hhGE 2984akD ...

  6. 1108. Finding Average (20)-PAT甲级真题

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  7. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  8. PAT甲级真题 1018 A+B in Hogwarts--python解法

    PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...

  9. [Java] 1031. Hello World for U (20)-PAT甲级

    1031. Hello World for U (20) Given any string of N (>=5) characters, you are asked to form the ch ...

最新文章

  1. 毛慧昀:决策树实现对鸢尾花数据集的分类
  2. Linux下使用两个线程协作完成一个任务的简易实现
  3. 【No.3 Ionic】超级逗表情 App
  4. 如何配置jenkins 与代理服务器吗?
  5. Java这些高端技术只有你还不知道,移动架构师成长路线
  6. s5pv210——串口(UART)通信实战
  7. 使用Collections.emptyList()生成的List不支持add方法___Java Collections.emptyList方法的使用及注意事项
  8. vue和小程序哪个好学一点_litemall,Spring Boot后端,微信小程序用户前端 + Vue用户移动端...
  9. 5G套餐月资费感受下:最低325元 仅提供8GB数据流量
  10. 关于浙大考研878历年试卷的说明
  11. 叫板英特尔,英伟达发布首个 CPU,集齐“三芯”!
  12. linux安装gd,linux下 安装GD
  13. 相机模型与标定(十三)--鱼眼相机标定
  14. 【安全科普】AD域安全管理(一)
  15. Win系统 - 全屏看视频时任务栏没有自动隐藏怎么办?
  16. 基于深度学习的单目2D/3D姿态估计综述(2021)
  17. 训练GAN的16个trick
  18. JavaScript+css+html鼠标指针经过某些元素时背景变色
  19. 数字电子技术-逻辑门电路
  20. 图论1:哥尼斯堡七桥问题的证明

热门文章

  1. 使用Data URI Scheme优雅的实现前端导出csv
  2. markdown的第一次使用
  3. 入职五年回顾(十五) 2013年10月
  4. 《并行计算的编程模型》一1.10 MPI开发心得
  5. js中“||”和“”的高级用法
  6. maven模块化分解项目
  7. 对于数组使用sizeof(a)和使用sizeof(a[0])
  8. .NET调用控制台下生成的exe文件,传参及获取返回参数
  9. 微软4月补丁星期二修复119个漏洞,含2个0day
  10. 从BMW Vision iNEXT 看宝马如何进军自动驾驶 1