写这段代码有32个目的,回忆一下N阶魔方状态数计算方法,使用java.math.BigInteger类,以及调试代码高亮插件。。。

废话不多说,上代码

/**
 * @author 欧阳韵奇(ouyang@yunqi.li)
 * @version 1.0
 * @date 2012-12-28
*/
import java.math.BigInteger;

public class CubeStateCounts {
    private static final BigInteger CONST_2 = new BigInteger("2");
    private static final BigInteger CONST_24 = new BigInteger("24");
    private static final BigInteger POW_2_11 = new BigInteger("2048");
    private static final BigInteger POW_3_7 = new BigInteger("2187");
    private static final BigInteger FACTOR_4 = new BigInteger("24");
    private static final BigInteger FACTOR_8 = new BigInteger("40320");
    private static final BigInteger FACTOR_12 = new BigInteger("479001600");
    private static final BigInteger FACTOR_24 = new BigInteger("620448401733239439360000");
    private static final int MIN_SIZE = 2;
    private static final int MAX_SIZE = 37;
    
    public static void main(String[] args) {
        int min = 0, max = 0;
        try {
            if (args.length < 1 || args.length > 2) {
                throw new Exception();
            }
            min = Integer.parseInt(args[0]);
            if (min < MIN_SIZE || min > MAX_SIZE) {
                throw new Exception();
            }
            max = (args.length == 2) ? Integer.parseInt(args[1]) : min;
            if (max < MIN_SIZE || max > MAX_SIZE) {
                throw new Exception();
            }
            if (max < min) {
                int t = max;
                max = min;
                min = t;
            }
        } catch (Exception e) {
            System.err.println("Usage:");
            System.err.println();
            System.err.println("java " + CubeStateCounts.class.getName() + " {min-size} [max-size]");
            System.err.println("where sizes are between " + MIN_SIZE + " and " + MAX_SIZE + ".");
            System.exit(1);
        }
        for (int i = min; i <= max; i++) {
            int odd = i & 1;
            BigInteger count = POW_3_7; //orientation of corners
            count = count.multiply(FACTOR_8); //permutation of corners
            if (odd == 1) {
                count = count.multiply(POW_2_11); //orientation of midges
                count = count.multiply(FACTOR_12); //permutation of midges
            }
            if (i > 3) {
                int k = (i - 2) >>> 1; //floor that, wing groups
                for (int t = 0; t < k; t++) {
                    count = count.multiply(FACTOR_24); //permutation of wings
                }
                k = ((i - 2) * (i - 2) - odd) >>> 2; //center groups
                for (int t = 0; t < k; t++) {
                    count = count.multiply(FACTOR_24);
                    for (int s = 0; s < 6; s++) {
                        count = count.divide(FACTOR_4); //permutation of centers
                    }
                }
            }
            if (odd == 1) {
                count = count.divide(CONST_2);
            } else {
                count = count.divide(CONST_24);
            }
            System.out.println(i + "阶魔方的总状态数为:" + count);
        }
    }
}
/* vim: set nofen: */