两层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());
}
}