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. ...
随机推荐
- JDK 7 API 下载
JDK 7 API 官方下载地址: http://www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download ...
- ASP.NET MVC CheckBoxFor为什么会生成hidden input控件
自己开发的公众号,可以领取淘宝内部优惠券 @Html.CheckBoxFor(m => m.Bool) 使用CheckBoxFor方法得到的html代码会是下面这个样子 <input ch ...
- 关于wav文件fft处理后x,y轴坐标数据的问题
1.关于横坐标的频率的最大值是采样频率,那么每个点对应的频率值就很好算了:f(n) = [Fs/(N/2)]*n (Fs是采样频率,常见的是44.1KHz(44100),N是采样点数,k表是第k个点 ...
- maven(多个模块)项目 部署 开发环境 问题处理历程【异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE】
maven(多个模块)项目 部署 开发环境 问题处理历程[异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE] 201 ...
- Mongodb简介及基本操作
一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性 ...
- echarts折线图相关
optionJKDLine = { title: { text: '告警数量趋势图', textStyle:{ //标题样式 fontStyle:'normal', fontFamily:'sans- ...
- 画报表框架——Echarts.js
官网:http://echarts.baidu.com/index.html ————————————————————————————————— 先看看我做的第一个柱状图形报表 ——————————— ...
- CheckPoint_vSEC_Cluster_R77.30
CheckPoint_vSEC_Cluster_R77.30 平台: arm 类型: ARM 模板 软件包: Check Point vSEC Gateway R77.30-041.161 Anti- ...
- 红象云腾CRH 一键部署大数据平台
平台: arm 类型: ARM 模板 软件包: azkaban hadoop 2.6 hbase hive kafka spark zeppelin azkaban basic software bi ...
- linux命令 ——目录
开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每天一个linux命令.学习的主要参考资料为: 1.<鸟哥的linux私房菜> 2.http://codingstan ...