论码农的道德修养
public class YH {
/**
* @author stefan_xiepj
* @param agrs
* 算法思想:分割分治算法,递归调用
*/
public static void main(String agrs[]) {
//测试main函数
int[][]test = subset(5);
for(int i=0;i<test.length;i++){
for(int j=0;j<test.length;j++){
System.out.print(test[i][j]+" ");
}
System.out.println();
}
}
public static int[][] subset(int n){
//定义二维数组长度
int length = (int) Math.pow(2, n);
//定义二维数组
int a[][] = new int [length][length];
//定义递归出口
if(length==2){
a[0][0]=1;
a[0][1]=2;
a[1][0]=a[0][1];
a[1][1]=a[0][0];
}else
//递归函数实现
{
//递归子函数调用
int [][]b = subset(n-1);
int bLength=b.length;
for(int i=0;i<bLength;i++){
for(int j=0;j<bLength;j++){
//复制下一递归值,并计算横向值
a[i][j] = b[i][j];
a[i][bLength+j] = (int) (b[i][j]+Math.pow(2, n-1));
}
}
//镜像结果集
for(int i=0;i<bLength;i++){
for(int j=0;j<length;j++){
a[length-i-1][length-j-1]=a[i][j];
}
}
}
return a;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
int n= scanner.nextInt();
int count=1;
for(;n>0;n--){
count=count<<1;
}
n=count;
StringBuffer sb=new StringBuffer();
for(int i=1;i<=count;i++){
sb.append(i+" ");
}
System.out.println(sb);
for(;n>1;n--)
sb=change(sb);
}
public static StringBuffer change(StringBuffer sb){
String s=sb.substring(0, 2);
sb.delete(0, 2);
sb.append(s);
System.out.println(sb);
return sb;
}
package zzQuestions;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;
/**
*/
public class RoundRobinClass {
/**
* 2的N次方个人
* 加入N=3,则输出
* 12345678 顺序排
* 21436587 顺序排,再2的0次方数交换
* 34127856 顺序排,再2的1次方数交换
* 43218765 顺序排,再2的1次方 交换,0次方 交换
* 56781234 顺序排,再2的2次方交换
* 65872143 顺序排,再2的2次方交换,0次方 交换
* 78563412 顺序排,再2的2次方交换,1次方 交换
* 87654321 顺序排,再2的2次方交换,1次方 交换,0次方 交换
* @param args
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int max = (int) Math.pow(2, n);
int[] orderArray = new int[max];
for (int i = 1; i <= max; i++) {
orderArray[i - 1] = i;
}
//这里可以考虑用for循环然后进行反射拿到方法执行。
for (int i = 0; i <= n; i++) {
String methodString = "outN" + i;
RoundRobinClass rc = new RoundRobinClass();
Method method = rc.getClass().getDeclaredMethod(methodString, int[].class);
method.invoke(rc, orderArray);
}
/*if (n >= 0) {
outN0(orderArray);//输出第1排
}
if (n >= 1) {
outN1(orderArray);//输出第2排
}
if (n >= 2) {
outN2(orderArray);//输出第3、4排
}
if (n >= 3) {
outN3(orderArray);//输出5、6、7、8排
}
if (n >= 4) {
outN4(orderArray);
}*/
}
/**
* 将orderArray中的元素依次进行ints个数交换后返回新Array,原orderArray不变,
* @param orderArray
* @param ints
*/
public static int[] getChangedArray(int[] orderArray, int... ints) {
int[] newArray = orderArray.clone();
for (int i : ints) {
changeArray(newArray, i);
}
return newArray;
}
/**
* 将orderArray中的元素依次每i个数交换后返回
* orderArray.length = 2的平方
* i<=orderArray.length/2 且 i=2的平法
* @param orderArray
* @param i
*/
private static void changeArray(int[] orderArray, int i) {
/*System.out.println("====================");
outN0(orderArray);
System.out.println(i);*/
for (int j = 0; j + i < orderArray.length; j = j + i * 2) {
for (int k = 0; k < i; k++) {
int c = orderArray[j + k];
orderArray[j + k] = orderArray[j + k + i];
orderArray[j + k + i] = c;
/*System.out.println("交换" + (j + k) + "与" + (j + k + i) + "后");
outN0(orderArray);*/
}
}
/*outN0(orderArray);
System.out.println("====================");*/
}
/**
* 输出原数据
*/
private static void outN0(int[] orderArray) {
for (int i : orderArray) {
System.out.print(i + "\t");
}
System.out.println();
}
/**
* 输出N=1在N=0上添加的行
* 交换2的【0】次方个数
*/
private static void outN1(int[] orderArray) {
int[] newArray = getChangedArray(orderArray, 1);
outN0(newArray);
}
/**
* 输出N=2在N=1上添加的行
* 交换2的【1,(1、0)】次方个数
*/
private static void outN2(int[] orderArray) {
int[] newArray1 = getChangedArray(orderArray, 2);
int[] newArray2 = getChangedArray(orderArray, 2, 1);
outN0(newArray1);
outN0(newArray2);
}
/**
* 输出N=3在N=2上添加的行
* 交换2的[2,(2、0),(2、1),(2、1、0)]次方个数
*/
private static void outN3(int[] orderArray) {
int[] newArray1 = getChangedArray(orderArray, 4);
int[] newArray2 = getChangedArray(orderArray, 4, 1);
int[] newArray3 = getChangedArray(orderArray, 4, 2);
int[] newArray4 = getChangedArray(orderArray, 4, 2, 1);
outN0(newArray1);
outN0(newArray2);
outN0(newArray3);
outN0(newArray4);
}
/**
* 输出N=4在N=3上添加的行
* 交换2的[3,(3、0),(3、1),(3、1、0),(3、2),(3、2、0),(3、2、1),(3、2、1、0)]次方个数
*/
private static void outN4(int[] orderArray) {
int[] newArray1 = getChangedArray(orderArray, 8);
int[] newArray2 = getChangedArray(orderArray, 8, 1);
int[] newArray3 = getChangedArray(orderArray, 8, 2);
int[] newArray4 = getChangedArray(orderArray, 8, 2, 1);
int[] newArray5 = getChangedArray(orderArray, 8, 4);
int[] newArray6 = getChangedArray(orderArray, 8, 4, 1);
int[] newArray7 = getChangedArray(orderArray, 8, 4, 2);
int[] newArray8 = getChangedArray(orderArray, 8, 4, 2, 1);
outN0(newArray1);
outN0(newArray2);
outN0(newArray3);
outN0(newArray4);
outN0(newArray5);
outN0(newArray6);
outN0(newArray7);
outN0(newArray8);
}
}
优化了一版,嘿嘿 。 真正的支持N个数字
package zzQuestions;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.springframework.util.Assert;
/**
* 循环赛制度
* @author xubo
*
*/
public class RoundRobinClass2 {
/**
* 2的N次方个人
* 加入N=3,则输出
* 12345678 顺序排
* 21436587 顺序排,再2的0次方数交换
* 34127856 顺序排,再2的1次方数交换
* 43218765 顺序排,再2的1次方 交换,0次方 交换
* 56781234 顺序排,再2的2次方交换
* 65872143 顺序排,再2的2次方交换,0次方 交换
* 78563412 顺序排,再2的2次方交换,1次方 交换
* 87654321 顺序排,再2的2次方交换,1次方 交换,0次方 交换
* @param args
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int max = (int) Math.pow(2, n);
int[] orderArray = new int[max];
for (int i = 1; i <= max; i++) {
orderArray[i - 1] = i;
}
outArray(orderArray); //输出顺序排列
for (int i = 1; i <= n; i++) { //依次输出N=1,N=2...N=N的增加的排列
List<StringBuffer> reN = getBufferList(i);
for (StringBuffer sb : reN) {
String[] stringArray = sb.toString().split(",");
int[] intArray = new int[stringArray.length];
for (int j = 0; j < intArray.length; j++) {
intArray[j] = Integer.parseInt(stringArray[j]);
}
//按照intArray交换并且输出
outArray(getChangedArray(orderArray, intArray));
}
}
}
/**
* 将orderArray中的元素依次进行2的ints平方值的个数交换后返回新Array,原orderArray不变,
* @param orderArray
* @param ints
*/
public static int[] getChangedArray(int[] orderArray, int... ints) {
int[] newArray = orderArray.clone();
for (int i : ints) {
changeArray(newArray, i);
}
return newArray;
}
/**
* 将orderArray中的元素依次每2的i平方值的个数交换后返回
* orderArray.length = 2的平方
* i<=orderArray.length/2 且 i=2的平法
* @param orderArray
* @param i
*/
private static void changeArray(int[] orderArray, int i) {
/*System.out.println("====================");
outN0(orderArray);
System.out.println(i);*/
i = (int) Math.pow(2, i);
for (int j = 0; j + i < orderArray.length; j = j + i * 2) {
for (int k = 0; k < i; k++) {
int c = orderArray[j + k];
orderArray[j + k] = orderArray[j + k + i];
orderArray[j + k + i] = c;
/*System.out.println("交换" + (j + k) + "与" + (j + k + i) + "后");
outN0(orderArray);*/
}
}
/*outN0(orderArray);
System.out.println("====================");*/
}
/**
* 输出原数据
*/
private static void outArray(int[] orderArray) {
for (int i : orderArray) {
System.out.print(i + "\t");
}
System.out.println();
}
/**
* 获取交换组合list,没个list中元素都是逗号分隔需要进行的平方
* N=1 ,则返回 0
* N=2,则返回 1,“1,0”
* N=n,则返回 n,再在N=1,2...n-1的基础上前面都加上n,
*/
private static List<StringBuffer> getBufferList(int N) {
Assert.isTrue(N > 0);
List<StringBuffer> result = new ArrayList<StringBuffer>();
result.add(new StringBuffer().append(N - 1).append(","));
if (N == 1) {
return result;
}
String newString = N - 1 + ",";
//System.out.println(newString);
for (int i = 1; i < N; i++) { //在前面从1到n-1的结果上依次加上N-1,
List<StringBuffer> tmp = getBufferList(i);
for (StringBuffer sbt : tmp) {
sbt.insert(0, newString);
result.add(sbt);
}
}
return result;
}
}