A1031. Hello World for U
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
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char str[], print[][];
int len, n1, n2, n3;
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
print[i][j] = ' ';
scanf("%s", str);
len = strlen(str);
n1 = n3 = (len + ) / ;
n2 = len + - n1 - n3;
int i;
for(i = ; i < n1; i++){
print[i][] = str[i];
}
for(int j = ; j < n2; j++, i++){
print[n1 - ][j] = str[i];
}
for(int k = n1 - ; k >= ; k--, i++){
print[k][n2 - ] = str[i];
}
for(int j = ; j < n1; j++){
for(int k = ; k < n2; k++)
printf("%c", print[j][k]);
printf("\n");
}
cin >> i;
return ; }
总结:
1、打印输出题两种方法,先将其处理在二维字符数组中再逐行输出。或者直接按规律打印输出。根据题意找规律很重要。
2、涉及到字符数组处理时可以使用string.h
A1031. Hello World for U的更多相关文章
- A1031 Hello World for U (20)(20 分)
A1031 Hello World for U (20)(20 分) Given any string of N (>=5) characters, you are asked to form ...
- PAT A1031 Hello World for U (20)
思路: 读取数组 int i = 0; while(cin >> word) { c[i] = word; i++; } 计算边长 int n1 = (length + 2) / 3; i ...
- A1031
画图,用二维数组作为画布 #include<cstdio> #include<string.h> int main(){ ],u[][]; scanf("%s&quo ...
- PAT甲级——A1031 Hello World for U
Given any string of N (≥) characters, you are asked to form the characters into the shape of U. For ...
- PAT/图形输出习题集
B1027. 打印沙漏 (20) Description: 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ...
- 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 chara ...
- PAT题目AC汇总(待补全)
题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
随机推荐
- html绝对路径,相对路径
.com/eat.php中引用.com/includes/headrt.php的话写includes/header.php .com/service/eat.php中引用.com/includes/h ...
- Nginx+upstream针对后端服务器容错的运维笔记
熟练掌握Nginx负载均衡的使用对运维人员来说是极其重要的!下面针对Nignx负载均衡upstream容错机制的使用做一梳理性说明: 一.nginx的upstream容错 1)nginx 判断节点失效 ...
- PHP从入门到精通(四)
PHP数组中的常用函数汇总 为了更直观的讲解各函数的作用和用法,方便大家的理解,首先,我们来定义一个数组.下面各函数的操作将以本数组为例: $arr = array(1,2,3,4,5,6," ...
- linux-流程控制语言
if: for: 增强for循环 while: 统计这个目录下所有文件的大小 编写脚本 执行 help text:
- bootstrap是什么
Bootstrap,来自 Twitter,是目前最受欢迎的前端框架. Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷. 本教程将向您讲解 ...
- C. Multi-Subject Competition
链接 [https://codeforces.com/contest/1082/problem/C] 题意 有n个人,m个科目,每个人都有选的科目si,以及他的能力值ri, 规则是每个科目要么选要么不 ...
- CF367C. Hard problem
链接[http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/C] 题意: 他希望它们按词典顺序排序(就像字典中那样),但他不允许交换其中 ...
- Individual Project-word frequency
预计时间: 项目要求理解:半小时 c#语言了解:6小时 构思程序框架:2小时 编写调试程序:4小时 项目实际完成时间: 项目要求理解:半小时 c#语言了解:6小时 构思程序框架:2小时 编写调试程序: ...
- android计算器
一:引言 目前手机可以说是普及率非常高的电子设备了,由于其便于携带,使用方便,资费适中等等原因,现在手机已经在一定程度开始代替固定电话的通话功能,以及一些原来电脑软件上的功能了.手机上的软件也随 ...
- 个人项目Individual Project:n皇后问题
源码的github链接: https://github.com/luhan420/test/tree/master 1.需求分析 在本次的课程设计中,用到的知识点主要有:类.函数.选择结构里的条件语 ...