分治策略:循环赛日程表问题

分治策略:循环赛日程表问题_第1张图片

分治策略:循环赛日程表问题_第2张图片分治策略:循环赛日程表问题_第3张图片分治策略:循环赛日程表问题_第4张图片


JAVA实现:

package suanfa.fenzhi;

import java.util.Scanner;

public class CircleTable {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		CircleTable table = new CircleTable(n);
		table.show();
	}
	
	private int[][] circleTable = null;
	
	public CircleTable(int n){
		circleTable = new int[n][n];
		circleTable[0][0] = 1;
		if(n>=2){
			circleTable[1][0] = 2;
			this.circle(n);
		}
	}

	private void circle(int i) {
		if(i==2){
			circleTable[0][i-1] = circleTable[i-1][0];
			circleTable[i-1][i-1] = circleTable[0][0];
		}else{
			circle(i/2);
			for (int j1 = i/2; j1 

分治策略:循环赛日程表问题_第5张图片


拓展阅读:如何判断用户输入的数是2的幂


其它:非分治法实现:

package suanfa.fenzhi;

import java.util.Scanner;

public class CircleTable {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		CircleTable table = new CircleTable(n);
		table.show();
	}
	
	private int[][] circleTable = null;
	
	public CircleTable(int n){
		circleTable = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < i; j++) {
				circleTable[i][j] = n-i+j+1;
			}
			for (int j = i; j < n; j++) {
				circleTable[i][j] = j-i+1;
			}
		}
	}
	
	public void show(){
		System.out.print("选手\t");
		for (int i = 1; i < circleTable.length; i++) {
			System.out.print("第"+i+"天\t");
		}
		System.out.println();
		for (int i = 0; i < circleTable.length; i++) {
			for (int j = 0; j < circleTable.length; j++) {
				System.out.print(circleTable[i][j]+"\t");
			}
			System.out.println();
		}
	}
	

}

分治策略:循环赛日程表问题_第6张图片

你可能感兴趣的:(算法设计方法)