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 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
思路
第二种方法,获取上界,编程就是解放人脑啊。
##AC代码
#include <stdio.h>
#include <string.h>
int main() {
char str[100], ans[40][40];//这里二维数组开到30也可以的吧
gets(str);//读入一行字符串
int N = strlen(str);//获取字符串的长度
int n1 = (N + 2) / 3, n3 = n1, n2 = N + 2 - n1 - n3;//自动向下取整
for(int i = 1; i <= n1; i++) {
for(int j = 1; j <= n2; j++) {
ans[i][j] = ' ';//初始化,将ans数组全部赋值为空格
}//第i行第j列
}
int pos = 0;
for(int i = 1; i <= n1; i++) {
ans[i][1] = str[pos++];
}
for(int j = 2; j <= n2; j++) {
ans[n1][j] = str[pos++];
}
for(int i = n3 - 1; i >= 1; i--) {
ans[i][n2] = str[pos++];
}
for(int i = 1; i <= n1; i++) {
for(int j = 1; j <= n2; j++) {
printf("%c", ans[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
gets(str);
int N = strlen(str);
int n1 = (N + 2) / 3, n3 = n1, n2 = N + 2 - n1 - n3;
for(int i = 0; i < n1 - 1; i++) {//前n1-1行
printf("%c", str[i]);
for(int j = 0; j < n2 - 2; j++) {
printf(" ");//n2-2个空格
}
printf("%c\n", str[N - 1-i]);
}
for(int i = 0; i < n2; i++) {
printf("%c", str[n1 + i - 1]);
}
return 0;
}
A1031 Hello World for U (20)(20 分)的更多相关文章
- MVC4 学习笔记 之 URL中存在编译的空格 20%20%
/Config/Edit/QQCC%20%20%20%20%20%20%20 原因是: 通过EF直接添加了空格? NO 是因为你的数据库字段设计问题,因为你当然设计如>:sID nchar(10 ...
- 2016年11月29日 星期二 --出埃及记 Exodus 20:20
2016年11月29日 星期二 --出埃及记 Exodus 20:20 Moses said to the people, "Do not be afraid. God has come t ...
- 安装nginx环境(含lua)时遇到报错ngx_http_lua_common.h:20:20: error: luajit.h: No such file or directory的解决
下面是安装nginx+lua环境时使用的相关模块及版本,ngx_devel_kit和lua-nginx-module模块用的都是github上最新的模块.并进行了LuaJIT的安装. #Install ...
- 做数据挖掘,就算发 20 几分的 CNS 子刊,也是垃圾!?--转载
关于数据挖掘发表文章,我们知道很多人是看不上.瞧不起.嗤之以鼻的.大抵是因为这些人平时只发 CNS 主刊,所以才认为通过数据挖掘这种用「别人的数据」或者叫「干实验」来发文章是“「垃圾」,没有什么价值. ...
- PAT A1031 Hello World for U (20)
思路: 读取数组 int i = 0; while(cin >> word) { c[i] = word; i++; } 计算边长 int n1 = (length + 2) / 3; i ...
- [C++]PAT乙级1012.数字分类 (20/20)
/* 1012. 数字分类 (20) 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和, ...
- [C++]PAT乙级1008.数组元素循环右移问题 (20/20)
/* 1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数, ...
- [C++]PAT乙级1007.素数对猜想 (20/20)
/* 1007. 素数对猜想 (20) 让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数.“素数对猜想”认为“存在无穷 ...
- [C++]PAT乙级1004. 成绩排名 (20/20)
/* 1004. 成绩排名 (20) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生 ...
- [C++]PAT乙级1002.写出这个数(20/20)
/* 1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100. ...
随机推荐
- dojo topic 发布与订阅 小例子可以参考下
<!DOCTYPE html><html> <head> <title></title></head> <body> ...
- c#读取word内容,c#提取word内容
Post by 54admin, 2009-5-8, Views:575 1: 对项目添加引用,Microsoft Word 11.0 Object Library 2: 在程序中添加 using W ...
- RabbitMQ:消息发送确认 与 消息接收确认(ACK)
默认情况下如果一个 Message 被消费者所正确接收则会被从 Queue 中移除 如果一个 Queue 没被任何消费者订阅,那么这个 Queue 中的消息会被 Cache(缓存),当有消费者订阅时则 ...
- java 读取环境变量和系统变量的方法
在web开发的过程中不免需要读取一些自定义的jvm系统变量或者环境变量.比如定义一些通用的log文件.或者数据库访问路径. 我们可以使用System.getProperties()读取所有的系统变量. ...
- centos yum 安装mysql5.7 以及 默认root密码查看
1. 首先更新rpm 从MySQL Yum仓库下载最新的rpm文件:http://dev.mysql.com/downloads/repo/yum/ (需要Oracle帐号以及填写一些使用信息,才能 ...
- agc007C - Pushing Balls(期望 等差数列)
题意 题目链接 翻译来自神仙yyb Sol 又是一道神仙题.. 我开始的思路是枚举空位,但是还是不能做,GG 标算过于神仙,其中一些细节我也理解不了 题目给出的实际是一个首项为$d$,公差为$x$的等 ...
- #include stdio.h(2)
#include <stdio.h> //mian函数是程序的入口 int main() { /* //函数:是按一定的格式对一段代码的封装 //专门用来实现一功能的代码合集,可以重复使用 ...
- defer=“defer”和async=“async”
<script type="text/javascript" src="demo_defer.js" defer="defer"> ...
- 关于com工程依赖的一些总结
作者:朱金灿 来源:http://blog.csdn.net/clever101 一是com组件工程的依赖设置.比如A这个组件工程要使用B组件工程的类,要如何设置呢?具体就是先把在A工程里加上B工程的 ...
- ArcGIS中合并空间有压盖关系的要素属性
1.前言 在客户单位, 被客户问道这样一个问题“如何合并两个有压盖关系图层的属性信息?” 在工具箱里面可以使用以下工具解决: 2.处理过程 (1)在工具箱中选择Spatial Join工具,并设置相关 ...