中国象棋将帅问题

来源:互联网 发布:虎头牌猎枪淘宝网 编辑:程序博客网 时间:2024/04/17 02:01

Method One:

没细看。

Method Two:

很容易理解的一种解法。使用了两个变量。

public class Chess {public static void main(String[] args) {// TODO Auto-generated method stubfor(int i=1;i<=9;i++){System.out.print("\n");for(int j=1;j<=9;j++)if(i%3!=j%3)System.out.print("A:"+ i +" B:" + j+"    ");}}}


输出如下:




Method Three:

这里有一句 n / 9 % 3 == n % 9 % 3 ,一眼过去没明白,但是稍一思考,就会发现,其实这就相当于在做循环了。

public class Chess_method3 {public static void main(String[] args) {// TODO Auto-generated method stubbyte n = 81;while(n-- !=0){if (n / 9 % 3 == n % 9 % 3)continue;System.out.println("A:" + (n / 9 + 1) + " B:" + (n % 9 + 1)+"   ");}}}

        输出不累赘展示了。


     这个解法的引申,到N层嵌套循环还没做细想。有时间再思考吧。

0 0
原创粉丝点击