硬币分类_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 硬币分类

硬币分类

 2014/6/16 16:09:14  treagzhao  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.main;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.HashSet;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importjava.util.Set;publicclassCoinCount{privateinttotalaccount=200;privateint[]values
  • 标签:
class="java" name="code">
package com.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CoinCount {
	private int totalaccount = 200;
	private int[] values;
	private static final int SORT_DIRECTION_ASCENDING = 0x1000;
	private static final int SORT_DIRECTION_DESCENDING = 0x1001;
	private Map<Integer, Integer> resultList = new HashMap();

	public int gettotalaccount() {
		return totalaccount;
	}

	public void settotalaccount(int tOTALACCOUNT) {
		totalaccount = tOTALACCOUNT;
	}

	public int[] getValues() {
		return values;
	}

	public void setValues(int[] values) {
		this.values = values;
	}

	public void calculate() {
		initValues();
		// values = new int[] { 46, 42, 42, 39, 27, 25, 9, 6, 1, 1 };
		values = sort(values, SORT_DIRECTION_DESCENDING);
		System.out
				.println("the target is :" + totalaccount + "\nthe input is:");
		for (int i = 0; i < values.length; i++) {
			System.out.print(values[i] + " ");
		}
		System.out.println();
		find(0, 0, 0);
		if (check()) {
			// 结果符合要求
			System.out.println("the result is :");
			for (int i = 0; i < resultList.size(); i++) {
				System.out.println(values[i] + "==>" + resultList.get(i));
			}
		} else {
			System.out.println("none result at all");
		}
	}

	private int find(int depth, int count, int totalCount) {
		// 边界条件
		if (depth >= values.length)
			return 0;
		int value = values[depth];
		if (value + totalCount == totalaccount) {
			resultList.put(depth, ++count);
			return 0;
		} else if (value > totalaccount) {
			return find(++depth, 0, totalCount);
		} else if (value + totalCount > totalaccount) {
			if (count < 1) {
				// 向下回溯
				resultList.put(depth, 0);
				value = find(++depth, 0, totalCount);
				return value;
			} else {
				resultList.put(depth, count);
				return find(++depth, 0, totalCount);
			}
		}
		totalCount += value;
		return find(depth, ++count, totalCount);
	}

	/**
	 * 初始化价格数组——纯粹是为了演示方便
	 */
	private void initValues() {
		values = new int[10];
		for (int i = 0; i < values.length; i++) {
			values[i] = (int) (Math.random() * 50 + 1);
		}
	}

	/**
	 * 数组排序
	 * 
	 * @param arr
	 * @param direction
	 *            排序方向 升序||降序
	 * @return
	 */
	public int[] sort(int arr[], int direction) {
		for (int i = 0; i < arr.length; i++) {
			for (int j = i + 1; j < arr.length; j++) {
				// 判断是升序还是降序
				boolean flag = false;
				if (direction == SORT_DIRECTION_ASCENDING) {
					flag = (values[j] < values[i]);
				} else if (direction == SORT_DIRECTION_DESCENDING) {
					flag = (values[j] > values[i]);
				}
				if (flag) {
					int temp = values[i];
					values[i] = values[j];
					values[j] = temp;
				}
			}
		}
		return values;
	}

	/**
	 * 检查结果是否正确
	 * 
	 * @return
	 */
	private boolean check() {
		boolean flag = false;
		int sum = 0;
		for (int i = 0; i < resultList.size(); i++) {
			sum += resultList.get(i) * values[i];
		}
		if (sum == totalaccount)
			flag = true;
		return flag;
	}
}

  • 相关文章
发表评论
用户名: 匿名