找到假币

//寻找假币

import java.util.Scanner;

public class test {
	static final int MAXNUM = 10;

	static int FalseCoin(int coin[], int low, int high) {
		int i, sum1 = 0, sum2 = 0, sum3 = 0;
		int re = 0;
		if (low + 1 == high) {
			if (coin[low] < coin[high]) {
				re = low + 1;
				return re;
			} else {
				re = high + 1;
				return re;
			}
		}

import java.util.Scanner;

public class test {
	static final int MAXNUM = 10;

	static int FalseCoin(int coin[], int low, int high) {
		int i, sum1 = 0, sum2 = 0, sum3 = 0;
		int re = 0;
		if (low + 1 == high) {
			if (coin[low] < coin[high]) {
				re = low + 1;
				return re;
			} else {
				re = high + 1;
				return re;
			}
		}

		if ((high - low + 1) % 2 == 0) {
			for (i = 0; i < (high - low + 1) / 2; i++) {
				sum1 += coin[low + i];
			}
			for (i = (high - low + 1) / 2; i > 0; i--) {
				sum2 += coin[low + i];
			}
			if (sum1 < sum2) {
				re = FalseCoin(coin, low, low + (high - low + 1) / 2 - 1);
				return re;
			}
			if (sum1 > sum2) {
				re = FalseCoin(coin, low + (high - low + 1) / 2, high);
				return re;
			} else {
				System.out.println("没有假币!");
			}
		} else {
			for (i = 0; i < (high - low) / 2; i++) {
				sum1 += coin[low + i];
			}
			for (i = (high - low) / 2; i > 0; i--) {
				sum2 += coin[low + i];
			}
			if (sum1 < sum2) {
				re = FalseCoin(coin, low, low + (high - low) / 2 - 1);
				return re;
			}
			if (sum1 > sum2) {
				re = FalseCoin(coin, low + (high - low) / 2 + 1, high);
				return re;
			}
		}
		return re;
	}

	public static void main(String[] arg) {
		int i;
		int coin[] = new int[MAXNUM];
		Scanner input = new Scanner(System.in);
		for (i = 0; i < MAXNUM; i++) {
			coin[i] = input.nextInt();
		}
		int re = FalseCoin(coin, 0, MAXNUM - 1);
		System.out.println(re);
	}
}

你可能感兴趣的:(java,分治算法)