九度 1464: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.
思路
1. N1+N2+N3-2= N, 尽量追求 N1,N2,N3 平均, 所以要么取 N3 = ceil((N+2)/3), 要么取 N1=N2=(N+2)/3-1. 第一种取法有不适合的情况
代码
#include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
using namespace std; int n1, n2, n3; int main() {
string input;
while(cin >> input) {
int len = input.size();
n1 = n2 = (len+)/ -;
n3 = len - *n1; for(int i = ; i < n1; i ++) {
cout << input[i];
for(int j = ; j < n3-; j ++) {
cout << ' ';
}
cout << input[len-i-];
cout << endl;
} for(int i = ; i < n3; i ++) {
cout << input[i+n1];
}
cout << endl;
}
return ;
}
九度 1464:Hello World for U的更多相关文章
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度 Online Judge 之《剑指 Offer》一书相关题目解答
前段时间准备华为机试,正好之前看了一遍<剑指 Offer>,就在九度 Online Judge 上刷了书中的题目,使用的语言为 C++:只有3题没做,其他的都做了. 正如 Linus To ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
随机推荐
- ES06--elasticsearch
ES06--elasticsearch unassigned错误解决(手动处理) 查看集群健康状态:curl -XGET http://localhost:9200/_cluster/health ...
- 【css】css 背景色渐变兼容写法
最近在项目中,有很多地方都用到了线性渐变,比如:表单提交按钮的背景,数据展示的标题背景等等,按照以前的做法是切 1px 图片然后 repeat-x.下面我将介绍如何用 css 来完成该效果. css3 ...
- 如何利用jsp实现防盗链功能
index.jsp ----------------------------- Place your content here here is index jsp get header info a. ...
- jQuery运行方式818
我们平时打开JQ源码就会看到这么一段代码 (function (window, undefined) { //JQ代码 })(window) 有一点经验的朋友会知道这是js自执行函数 它的好处主要作用 ...
- JavaScript 作用域链解析
JavaScript 中有 Scope( 作用域 ) , Scope chain( 作用域链 ) , Execute context( 执行上下文 ) , Active Object ( 活动对象 ) ...
- tensorflow的升级与版本管理
1 查看cuda与cudnn的版本 cuda一般安装在 /usr/local/cuda/ 路径下,该路径下有一个version.txt文档,里面记录了cuda的版本信息 cat /usr/local ...
- 第三百七十节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索结果分页
第三百七十节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现搜索结果分页 逻辑处理函数 计算搜索耗时 在开始搜索前:start_time ...
- vector 去除重复元素(sort + unique)
struct SIndexData { string sCode; int iDate; int iTime; double f1; SIndexData(): iDate(0), iTime(0) ...
- MyBatis中对于字符串blank(null、empty)的判定方法
直接上代码,关键需要进行2个判定,一个是null判定,一个是 ‘’ 判定. <where> <if test="url!= null and url!=''"&g ...
- Yii2.0 to(), toRoute(), current()区别用法
[Url::current] - 现在测试本地路径(http://daxia.dc.weixin.com/mobile/hmConnections/user/verify-user) a: //获取当 ...