编程算法基础-3.1自顶向下风格

来源:互联网 发布:淘宝嘉年华免邮 编辑:程序博客网 时间:2024/05/17 07:51

第三讲 风格与模式

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();}}}
┌─┬─┬─┐│ │ │ │├─┼─┼─┤│ │ │ │└─┴─┴─┘
0 0
原创粉丝点击