程式設計演算法基礎-3.1自頂向下風格
阿新 • • 發佈:2019-02-07
第三講 風格與模式
3.1自頂向下風格
複雜問題分解,直到小問題足夠簡單,可以掌控為止
是一種思考方式
把大的任務不斷的分解為更小的子任務
另一法寶:忽略,忽略細節
程式問題
制定框架---》逐步細化---》逐步精華---》分解為子問題
列印特定的形狀
做一個二維陣列的緩衝區
向緩衝區輸出
緩衝區輸出螢幕上
陣列初始元素都是0
表格:橫線,豎線。
/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */ package topToBottom; public class Print { public static void main(String[] args) { char[][] a = new char[20][50]; for(int i=0;i<4;i++){ line_h(a, i*3, 0, 32); } for(int i=0;i<9;i++){ line_v(a, i*4, 0, 9); } print(a); } //寫入列 public static void line_v(char [][]a,int col,int row_start,int row_end){ for(int i=row_start;i<=row_end;i++){ a[i][col] = '$'; } } //寫入行 public static void line_h(char [][]a,int row,int col_start,int col_end){ for(int i=col_start;i<=col_end;i++){ a[row][i] = '$'; } } //列印 public static void print(char[][] a){ for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ if(a[i][j]==0){ System.out.print(" "); }else{ System.out.print(a[i][j]); } } System.out.println(); } } }
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Word排版問題,沒有對齊
設計程式-列印
設計程式-列印 /* 設計程式 在中文Windows環境下,控制檯視窗中也可以用特殊符號拼出漂亮的表格來。 比如: ┌─┬─┐ │ │ │ ├─┼─┤ │ │ │ └─┴─┘ 其實,它是由如下的符號拼接的: 左上 = ┌ 上 = ┬ 右上 = ┐ 左 = ├ 中心 = ┼ 右 = ┤ 左下= └ 下 = ┴ 右下 = ┘ 垂直 = │ 水平 = ─ 本題目要求編寫一個程式,根據使用者輸入的行、列數畫出相應的表格來。 例如使用者輸入: 3 2 則程式輸出: ┌─┬─┐ │ │ │ ├─┼─┤ │ │ │ ├─┼─┤ │ │ │ └─┴─┘ 使用者輸入: 2 3 則程式輸出: ┌─┬─┬─┐ │ │ │ │ ├─┼─┼─┤ │ │ │ │ └─┴─┴─┘ */ package topToBottom; public class Code { public static void main(String[] args) { int a = 2;//行數 int b = 3;//列數 createArray(a, b); } //建立陣列 public static void createArray(int a,int b){ char[][] c = new char[2*a+1][2*b+1]; c[0][0] = '┌';//左上 c[0][2*b+1-1] = '┐';//右上 c[2*a+1-1][0] = '└';//左下 c[2*a+1-1][2*b+1-1] = '┘';//右下 firstAndLastRow(c, a, b); singleRow(c, a, b); doubleRow(c, a, b); singleColumn(c, a, b); printGrid(c,a,b); } //第奇數列 public static char[][] singleColumn(char[][] c,int a,int b){ for(int i=0;i<2*a+1;i+=2){ for(int j=1;j<2*b;j+=2){ c[i][j]='─'; } } return c; } //第偶數行 public static char[][] doubleRow(char[][]c,int a,int b){ for(int i=2;i<2*a-1;i+=2){ c[i][0] = '├'; c[i][2*b] = '┤'; for(int j=2;j<2*b;j+=2){ c[i][j] = '┼'; } } return c; } //第奇數行 public static char[][] singleRow(char[][]c,int a,int b){ for(int i=1;i<2*a;i+=2){ for(int j=0;j<2*b+1;j+=2){ c[i][j]='│'; } } return c; } //第一行和最後一行 public static char[][] firstAndLastRow(char[][]c,int a,int b){ for(int i=2;i<2*b+1-1;i+=2){//第一行和最後一行 c[0][i] = '┬'; c[2*a+1-1][i] = '┴'; } return c; } //打印表格 public static void printGrid(char[][] c,int a,int b){ for(int i=0;i<2*a+1;i++){ for(int j=0;j<2*b+1;j++){ System.out.print(c[i][j]); } System.out.println(); } } }
┌─┬─┬─┐
│ │ │ │
├─┼─┼─┤
│ │ │ │
└─┴─┴─┘