两层for循环实现右上角打印

4 篇文章 1 订阅

两层for循环实现右上角打印的六种不同顺序

第一种顺序实现

效果图如下:

 /**
     * 0    1   2   3
     * 0    0   4   5
     * 0    0   0   6
     * @return
     */
public static int[][] show1(){
        int[][] result  = new int[len+1][len+1];
        int a =1;
        for(int i=0;i<=len;i++){
            for(int j=i+1;j<=len;j++){
                result[i][j]=a;
                a++;
            }
        }
       return result;
    }

第二种顺序实现

效果图如下:

/**
     * 0    1   2   4
     * 0    0   3   5
     * 0    0   0   6
     * @return
     */
public static int[][] show2(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int j=1;j<=len;j++){
            for(int i=0;i<j;i++){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }

第三种顺序实现

效果图如下:


    /**
     * 0    1   3   6
     * 0    0   2   5
     * 0    0   0   4
     * @return
     */
public static int[][] show3(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int j=1;j<=len;j++){
            for(int i=j-1;i>=0;i--){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }

第四种顺序实现

效果图如下:

/**
     * 0    6   5   3
     * 0    0   4   2
     * 0    0   0   1
     * @return
     */
    public static int[][] show4(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int j=len;j>0;j--){
            for(int i=j-1;i>=0;i--){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }

第五种顺序实现

效果图如下:

/**
     * 0    6   5   4
     * 0    0   3   2
     * 0    0   0   1
     * @return
     */
  public static int[][] show5(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int i=len-1;i>=0;i--){
            for(int j=len;j>i;j--){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }

第六种顺序实现

效果图如下:

/**
     * 0    4   5   6
     * 0    0   2   3
     * 0    0   0   1
     * @return
     */
public static int[][] show6(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int i=len-1;i>=0;i--){
            for(int j=i+1;j<=len;j++){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }

总结方法

/**
     * 口诀:
     * 1.开始位置在头,其横着走:for循环先i后j,否则先j后i
     * 开始位置在尾,其竖着走:for循环先j后i,否则先i后j
     * 2.同j列,看i行往下走是加加,否则是减减
     * 同i行,看j列往右走是加加,否则是减减
     * 3.如果是i(j)加加,需要判断是小于等于len 还是小于等于j(i)
     * 如果是i(j)减减,需要判断是大于等于0,还是大于等于j(i)
     * 4.初始化和3步骤的比较判断看开始位置的坐标
     */

其他话题

上面实现的for循环部分是无后效性的,其无后效性会在动态规划的算法上显示出它的重要性。有兴趣的读者自行去了解的。

最终代码

package cn.chenjb.mall.day;

public class ForDemo {
    public static final int len =3;

    /**
     * 0    1   2   3
     * 0    0   4   5
     * 0    0   0   6
     * @return
     */
    public static int[][] show1(){
        int[][] result  = new int[len+1][len+1];
        int a =1;
        for(int i=0;i<=len;i++){
            for(int j=i+1;j<=len;j++){
                result[i][j]=a;
                a++;
            }
        }
       return result;
    }

    /**
     * 0    1   2   4
     * 0    0   3   5
     * 0    0   0   6
     * @return
     */
    public static int[][] show2(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int j=1;j<=len;j++){
            for(int i=0;i<j;i++){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }

    /**
     * 0    1   3   6
     * 0    0   2   5
     * 0    0   0   4
     * @return
     */
    public static int[][] show3(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int j=1;j<=len;j++){
            for(int i=j-1;i>=0;i--){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }

    /**
     * 0    6   5   3
     * 0    0   4   2
     * 0    0   0   1
     * @return
     */
    public static int[][] show4(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int j=len;j>0;j--){
            for(int i=j-1;i>=0;i--){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }
    /**
     * 0    6   5   4
     * 0    0   3   2
     * 0    0   0   1
     * @return
     */
    public static int[][] show5(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int i=len-1;i>=0;i--){
            for(int j=len;j>i;j--){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }
    /**
     * 0    4   5   6
     * 0    0   2   3
     * 0    0   0   1
     * @return
     */
    public static int[][] show6(){
        int[][] result = new int[len+1][len+1];
        int a=1;
        for(int i=len-1;i>=0;i--){
            for(int j=i+1;j<=len;j++){
                result[i][j]=a;
                a++;
            }
        }
        return result;
    }
    /**
     * 口诀:
     * 1.开始位置在头,其横着走:for循环先i后j,否则先j后i
     * 开始位置在尾,其竖着走:for循环先j后i,否则先i后j
     * 2.同j列,看i行往下走是加加,否则是减减
     * 同i行,看j列往右走是加加,否则是减减
     * 3.如果是i(j)加加,需要判断是小于等于len 还是小于等于j(i)
     * 如果是i(j)减减,需要判断是大于等于0,还是大于等于j(i)
     * 4.初始化和3步骤的比较判断看开始位置的坐标
     */
    
    public static void printShow(int[][] req ){
        int len = req.length;
        System.out.println("------------------------");
        for(int i=0;i<len;i++){
            for(int j=0;j<len;j++){
                System.out.print(req[i][j]+"\t");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        printShow(show1());
        printShow(show2());
        printShow(show3());
        printShow(show4());
        printShow(show5());
        printShow(show6());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值