题目描述:

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的更多相关文章

  1. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  2. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  3. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  4. 九度 Online Judge 之《剑指 Offer》一书相关题目解答

    前段时间准备华为机试,正好之前看了一遍<剑指 Offer>,就在九度 Online Judge 上刷了书中的题目,使用的语言为 C++:只有3题没做,其他的都做了. 正如 Linus To ...

  5. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  6. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  7. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  8. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

  9. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

随机推荐

  1. Material Design 相关资源

    Materialpalette -- Material配色工具 Materialup -- Material设计灵感: Material Design 相关好文: <超全面总结!深聊MATERI ...

  2. Hibernate执行原生SQL

    1.查询指定字段 public List<Object[]> getUseList( Integer index, Integer offset, String state, String ...

  3. 【协议学习】SIP基本场景分析

    1.SIP业务基本知识 1.1 业务介绍 会话初始协议(Session Initiation Protocol)是一种信令协议,用于初始.管理和终止网络中的语音和视频会话,具体地说就是用来生成.修改和 ...

  4. Item is not readable svn: 条目不可读

    问题:svn 查看资源历史记录失败 ,并提示"Item is not readable" 解决: 配置目录权限时如: [/]tangtx=rwyangcx=rwweishq=rw ...

  5. DataFrame在算术方法中填充值

    在对不同索引的对象进行算术运算时, 你可能希望当一个对象中某个轴标签在另一个对象中找不到时填充一个特殊值(比如0)

  6. 【Python】python3实现网页爬虫下载图片

    import re import urllib.request # ------ 获取网页源代码的方法 --- def getHtml(url): page = urllib.request.urlo ...

  7. InteliJ Idea pom.xml不自动提示的解决

    file–>Settings–>Build,Execute…–>Build Tools–>Maven–>Repositories 会看到类似表格的画面,内容是你的mave ...

  8. C#跳转网页7种方法

    1.Response.Redirect(http://www.baidu.com,false); 目标页面和原页面可以在2个服务器上,可输入网址或相对路径.后面的bool值为是否停止执行当前页. 跳转 ...

  9. Java处理JSON的工具类(List、Map和JSON之间的转换)——依赖jsonlib支持Map嵌套

    原文链接:http://www.itjhwd.com/java_json/ 代码 package com.itjh.mmp.util; import java.io.BufferedReader; i ...

  10. python3 post方式上传文件。

    借助第三方库:Requests 其官网地址:   http://python-requests.org       官网上写的安装方式:http://docs.python-requests.org/ ...