編程之美——象棋將帥問題


解法一:


public class Chess_Test {
    
    public static void main(String[] args) throws InterruptedException{
        long t1 = System.currentTimeMillis();
        
        Byte i = 81;
        while(i-- > 0){
            if(i / 9 % 3 == i % 9 % 3)
                continue;
            System.out.println(String.format("A = %d, B = %d\n", i / 9 + 1, i % 9 + 1));
        }
        System.out.println(System.currentTimeMillis()-t1);
        
    }
}

解法二:




public class Chess_Test2 {
    
    class Point{
        char a;
        char b;
    }
    
    public static void main(String[] args) throws InterruptedException{
        long t1 = System.currentTimeMillis();
        Point p = new Chess_Test2().new Point();
        for(p.a = 1; p.a <= 9; p.a++){
            for(p.b = 1; p.b <= 9; p.b++){
                if(p.a % 3 == p.b % 3){
                    continue;
                }
                System.out.println(String.format("A = %c, B = %c\n", p.a+'0', p.b+'0'));
            }
        }
        System.out.println(System.currentTimeMillis()-t1);
        
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章