硬币分类

本文介绍了一个硬币计数的算法实现,该算法通过递归的方式找出一系列数值中能够组合成目标总额的所有可能组合,并验证这些组合是否准确。文章详细展示了如何初始化数值数组、进行排序、递归查找及验证结果的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值