算法

public class Test
{   
    public static void main(String[] args)
    {
        if(args.length != 3){
            return;
        }
        int start = Integer.parseInt(args[0]);
        int end = Integer.parseInt(args[1]);
        int limit = Integer.parseInt(args[2]);
        
        //二维数组
        int[][] rows = new int[limit][];
        for (int i = 0; i < rows.length; i++)
        {
            rows[i] = new int[limit-i];
        }
        //赋值,行列互换
        for (int x = 0; x < rows.length; x++)
        {
            for (int y = 0; y < rows[x].length; y++)
            {
                rows[y][x] = start;
                start ++;
                //reset
                if(start > end){
                    start = 1;
                }
            }
        }
        //输出
        for (int x = 0; x < rows.length; x++)
        {
            for (int y = 0; y < rows[x].length; y++)
            {
                if(y != 0){
                    System.out.print(" ");
                }
                System.out.print(rows[x][y]);
            }
            //new line
            System.out.println();
        }
    }
}

 

 

public class Test2
{   
    public static void main(String[] args)
    {
        if(args.length != 3){
            return;
        }
        int start = Integer.parseInt(args[0]);
        int end = Integer.parseInt(args[1]);
        int limit = Integer.parseInt(args[2]);
        
        //初始化 [1,2,3,4,5]
        int[] array = new int[end];
        for (int i = 0; i < end; i++)
        {
            array[i] = i+1;
        }
        
        int index = start - 1;
        for (int row = 0; row < limit; row++)
        {
            for (int step = limit; step > row; step--)
            {
                if(step != limit){
                    System.out.print(" ");
                }
                //读取array中index位置的值
                System.out.print(array[index]);
                //调整步长,循环读取
                index = (index + step) % end;
            }
            //每行重新调整索引
            index ++;
            if(index >= array.length){
                index = 0;
            }
            //换行
            System.out.println();
        }
    }
}

 

posted on 2013-10-30 17:26  LoveEyes  阅读(246)  评论(0编辑  收藏  举报

导航