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. ...
随机推荐
- RPC框架设计思路
RPC是指远程过程调用 1.要解决通讯的问题,主要是通过在客户端和服务器之间建立TCP连接,远程过程调用的所有交换的数据都在这个连接里传输.连接可以是按需连接,调用结束后就断掉,也可以是长连接,多个远 ...
- 360或其他双核浏览器下在兼容模式用chrome内核渲染的方法
<meta name="renderer" content="webkit"> <meta http-equiv="X-UA-COM ...
- js数字滑动时钟
js数字滑动时钟: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- node模拟后台返回json书写格式报错--Unexpected token ' in JSON at position 1
最近在学习Node的知识,就尝试写了一个注册登陆的简单功能,但是自己在模拟后台返回值的时候,总是报错Unexpected token ' in JSON at position 1,查找原因之后,是因 ...
- redis空间键详解
前言 redis的空间键通知是在2.8.0版本以后加入的,客户端通过发布订阅的方式,订阅某个频道,接收通过某种方式影响redis中数据的事件. 目录: 1.空间键事件分类 2.如何启用redis的空间 ...
- 平时对ES6的一些总结
1.Genertor中yield和Interator中的next方法 Genertor的yield是把这个函数变成分段的:Interator中的next也是一个一个执行的: function* f() ...
- 初见微服务之RESTful API
1. REST名称由来 REST全称为Representational State Transfer,即表述性状态转移,最早由Roy Feilding博士在世纪之交(2000年)提出,喜欢追根溯源的朋 ...
- 定义多个属性 Object.defineProperties()
var book = {} Object.defineProperties(book,{ _year:{ value:2004 }, editable:{ value:1 }, year:{ get: ...
- NYOJ-22-素数求和问题
原题地址 素数求和问题 描述 现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和. 输入 第一行给出整数M(0<M<10)代表多少组测 ...
- Java中ArrayList的对象引用问题
前言事件起因是由于同事使用ArrayList的带参构造方法进行ArrayList对象复制,修改新的ArrayList对象中的元素(对象)的成员变量时也会修改原ArrayList中的元素(对象)的成员变 ...