码迷,mamicode.com
首页 > 其他好文 > 详细

Print an NxM matrix with nw-se diagonals

时间:2017-12-07 14:57:55      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:bsp   amp   public   rgs   main   count   put   code   static   

Print an NxM matrix with nw-se diagonals starting at bottom left corner. Ex:


1  2  3  4
5  6  7  8
9 10 11 12
The output should be:


9
5 10
1 6 11
2 7 12
3 8
4

 

import java.io.*;

class myCode
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int n = 3;
        int m = 4;
        int arr[][] = new int[n][m];
        arr[0] = new int[] { 1, 2, 3, 4 };
        arr[1] = new int[] { 5, 6, 7, 8 };
        arr[2] = new int[] { 9, 10, 11, 12 };
        
        //ist=>i_start
        //jst=>j_start
        int i=n-1,j=0,ist,jst,count=0;
        while(count<n*m){
            ist=i;jst=j;
            while(i<n && j<m){
                System.out.print(arr[i][j]+" ");
                count++;
                i++;j++;
            }
            System.out.println();
            i=(ist-1)>=0?ist-1:ist;
            j=(jst==0 && (ist-1)>=0)?0:jst+1;
        }
    }
}

  

Print an NxM matrix with nw-se diagonals

标签:bsp   amp   public   rgs   main   count   put   code   static   

原文地址:http://www.cnblogs.com/apanda009/p/7998607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!