【算法】程序猿不写代码是不对的71
2017-06-15 17:43
204 查看
package com.kingdz.algorithm.time201706; /** * <pre> * 打印U型字符串 * * http://judgecode.com/problems/1003 * * Given a string of length n, please print it into shape of U. * 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 satisfy that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = n. * For example the word “helloworld” should be: * * h d * e l * l r * lowo * </pre> * * @author kingdz * */ public class Algo15 { public static void main(String[] args) { String input = "helloworldnihaoma"; int length = input.length(); int n1 = length / 3; int n3 = length - n1 - n1 - 2; StringBuilder strb = new StringBuilder(); char[] strArray = input.toCharArray(); for (int i = 0; i < n1 + 1; i++) { if (i == n1) { // 特殊处理最后一行 strb.append(input.substring(i, length - i)); break; } char left = strArray[i]; char righ = strArray[length - i - 1]; strb.append(left); for (int j = 0; j < n3; j++) { strb.append(" "); } strb.append(righ); strb.append("\n"); } System.out.println(strb.toString()); } }
相关文章推荐
- 【算法】程序猿不写代码是不对的3
- 【算法】程序猿不写代码是不对的6
- 【算法】程序猿不写代码是不对的26
- 【算法】程序猿不写代码是不对的32
- 【算法】程序猿不写代码是不对的31
- 【算法】程序猿不写代码是不对的1
- 【算法】程序猿不写代码是不对的36
- 【算法】程序猿不写代码是不对的22
- 【算法】程序猿不写代码是不对的21
- 【算法】程序猿不写代码是不对的34
- 【算法】程序猿不写代码是不对的7
- 【算法】程序猿不写代码是不对的13
- 【算法】程序猿不写代码是不对的12
- 【算法】程序猿不写代码是不对的24
- 【算法】程序猿不写代码是不对的18
- 【算法】程序猿不写代码是不对的5
- 【算法】程序猿不写代码是不对的11
- 【算法】程序猿不写代码是不对的15
- 【算法】程序猿不写代码是不对的29
- 【算法】程序猿不写代码是不对的35