关闭

Hello World for U

标签: 浙大2012-1
64人阅读 评论(0) 收藏 举报
分类:
import java.util.Scanner;
public class U{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String[] input = new String[1];
        input[0] = scan.nextLine();
        int i = 1;
        while(scan.hasNext()){
            input = enlargeArray(input);
            input[i] = scan.nextLine();
            i++;
        } 
        //System.out.println(input.length);
        for(int t=0;t<input.length;t++) 
                out(input[t]);      

    }

    //扩大数组算法
    public static String[] enlargeArray(String[] input){
        int length = input.length;
        String[] temp = new String[length+1];
        for(int i=0;i<length;i++)
            temp[i] = input[i];
        input = temp;
        return input;
    }

    public static void out(String input){
        int n1=0,n2=0,n3=0;
        int minresult = 0;
        int length = 0;
        length =input.length();
        //System.out.println(length);
        String[] n = new String[3];
        minresult = (length+2)/3;
        //System.out.print("minresult: "+minresult);
        if((length+2)%3==0)
            n1=n2=n3 = minresult;   
        else{
            n1=1;
            n2=2;
            n3=minresult+1;
            while(n1!=n2){
                n1 = (length+2-n3)/2;
                n2 = length+2-n1-n3;
                if(n1!=n2)
                    n3++;
            }
        }

        //System.out.println("n1: "+n1+" n2:"+n2+" n3:"+n3);
        n[0] = input.substring(0,n1-1);
        n[1] = input.substring(n1-1,n1+n3-1);
        n[2] = input.substring(n1+n3-1,n1+n2+n3-2);
        //System.out.println("n[0]: "+n[0]+" n[1]: "+n[1]+" n[2]: "+n[2]);
        int k = n[2].length()-1;
        for(int i=0;i<n[0].length();i++){
            System.out.print(n[0].charAt(i));
            for(int j=0;j<n3-2;j++)
                System.out.print(" ");
            System.out.println(n[2].charAt(k--));
        }
        System.out.println(n[1]);

    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:735次
    • 积分:39
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档