今天闲着没事,想起了学校时学C语言时候印象最深的两个程序:打印99乘法表和求9宫图的解。99乘法表那个,让我知道了原来for还可以两层嵌套用;而9宫图的那个,当时写的是一个4x4的(其实是16宫),在学校的破机器上运行了2分钟多,得到了几千个解;记得张大傻(嘿嘿,别打我)当时还不相信有这么多的结果。丫的,好久没这厮的消息了。
怀念在学校里的生活!怀念玩红警魔兽、踢足球的日子!
//99乘法表
public class Table9mult9 {
public static void main(String[] args) {
for(int i =1 ;i<10 ;i++){
for(int j= 1; j<10 ;j++){
if(j<=i)
System.out.print(j +"*" + i +"=" + (i*j) +" " ) ;
if(i==j)
System.out.print("/n") ;
}
}
}
}
//九宫图求解
public class Grid9{
private int average ; //将1-9 填到9个格子里,每个格子的平均数
private int avg3Times ;
private int[] grid = new int[9]; //9个格子
private String stars ;
private int resultsNum ;
private static int getAverage(){
int sum = 0 ;
for(int i = 1;i<=9 ;i++){
sum += i;
}
return sum /9 ;
}
public Grid9(){
resultsNum = 0 ;
average = getAverage() ;
avg3Times = average * 3 ;
stars = "--------------------" ;
for(int i=0;i<9;i++){
grid[i] = 0 ;
}
}
//构造gird
public void generateGrid(int i,int j,int k ,
int ii,int jj,int kk,int iii,int jjj,int kkk){
if (i!=j && i!=k && i!= ii && i!=jj && i!=kk && i!=iii && i!= jjj && i!=kkk
&& j != k && j!=ii && j!= jj && j!= kk && j!=iii && j!=jjj && j!= kkk
&& k !=ii && k!=jj && k!= kk && k!=iii && k!=jjj && k!=kkk
&& ii!=jj && ii!=kk && ii!= iii && ii!=jjj && ii!= kkk
&& jj!=kk && jj!=iii && jj!=jjj && jj!= kkk
&& kk!=iii && kk!= jjj && kk!= kkk
&& iii!=jjj && iii!= kkk
&& jjj!=kkk
){
grid[0] = i ;
grid[1] = j ;
grid[2] = k ;
grid[3] = ii ;
grid[4] = jj ;
grid[5] = kk ;
grid[6] = iii ;
grid[7] = jjj ;
grid[8] = kkk ;
}else{
for (int index = 0; index < 9; index++) {
grid[index] = 0 ;
}
}
}
//打印 grid
public void printGrid(){
for (int i = 1; i <= 9; i++) {
System.out.print(grid[i-1] + " ") ;
if (i%3==0){
System.out.print("/n") ;
}
}
System.out.println(stars) ;
}
//得到第一行的和,以下依次
public int get1Row(){
return grid[0] +grid[1]+grid[2] ;
}
public int get2Row(){
return grid[3] +grid[4]+grid[5] ;
}
public int get3Row(){
return grid[6] +grid[7]+grid[8] ;
}
//得到列的和
public int get1Col() {
return grid[0] +grid[3]+grid[6] ;
}
public int get2Col() {
return grid[1] +grid[4]+grid[7] ;
}
public int get3Col() {
return grid[2] +grid[5]+grid[8] ;
}
//得到交叉的和
public int get1X (){
return grid[0] +grid[4]+grid[8] ;
}
public int get2X (){
return grid[2] +grid[4]+grid[6] ;
}
//判等:每行==每列==交叉的和
public boolean isEqual(){
return (get1Row()==avg3Times && get2Row()==avg3Times && get1Col()== avg3Times
&& get2Col() ==avg3Times &&get1X()==avg3Times && get2X() == avg3Times ) ;
}
//得到结果的个数:
public int getResultsNum(){
return resultsNum ;
}
//最笨的办法——遍历
public void getResults(){
for (int i = 1; i < 10 ; i++) {
for (int j = 1; j < 10; j++) {
for (int k = 1; k < 10; k++) {
for (int ii = 1; ii < 10; ii++) {
for (int jj = 1; jj < 10; jj++) {
for (int kk = 1; kk < 10; kk++) {
for (int iii = 1; iii < 10; iii++) {
for (int jjj = 1; jjj < 10; jjj++) {
for (int kkk = 1; kkk < 10; kkk++) {
generateGrid(i, j, k, ii, jj, kk, iii, jjj, kkk) ;
if( isEqual()){
printGrid () ;
resultsNum ++ ;
}
}
}
}
}
}
}
}
}
}
}
public static void main(String[] args) {
Grid9 grid9 = new Grid9();
System.out.println("starting...") ;
grid9.getResults() ;
System.out.println("end ") ;
System.out.println("results number: " + grid9.getResultsNum() ) ;
}
}