把一元钞票换成一分、二分、五分硬币(每种至少一枚),有多少种换法?---使用递归

四维兜兜 2012-10-22 11:45:28
我用3个for循环嵌套做了一次,但老师要求用递归,我是菜鸟,不太明白递归怎么用,求高手们指点,先谢谢啦~
...全文
3725 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
s63403048 2012-11-15
  • 打赏
  • 举报
回复
引用 14 楼 cowboyhn 的回复:
按照面值从大到小处理,一张张累加,剩下的能被最小面值整除,则是一种组合。 Java code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071public……
学习了..
四维兜兜 2012-10-25
  • 打赏
  • 举报
回复
感谢14楼朋友的代码,更加感谢1楼的朋友耐心的指点,我按照你的方法写了一遍,运行成功了,代码在上一层,真心感谢,学习了,递归啊,递归....
四维兜兜 2012-10-25
  • 打赏
  • 举报
回复
public class Money_digui_2 {

/**
* @param args
*/
private static final int LEN = 3;

static int[] a={1,2,5};

public static void main(String[] args) {
System.out.println("种数="+pay(100,0));
}

public static int remain(int i){
//求出当前未使用的钞票之和 7 5
int total = 0;
for(int j=LEN-1;j>i;j--)
total += a[j];
return total;
}

private static int pay(int total, int i) {
int count = 0;
if(i>2){
if(total == 0){
//当总钱数减完时,一种方案完成,返回1
return 1;
}
return 0;
}
else {
//判断余额是否小于于等于没用的钞票,如果没有,不能再选当前的钞票了
for(int n=1;total-n*a[i]>=remain(i);n++){
count += pay(total-n*a[i],i+1);
}
}
return count;
}
cowboyhn 2012-10-25
  • 打赏
  • 举报
回复
按照面值从大到小处理,一张张累加,剩下的能被最小面值整除,则是一种组合。

public class PayType {

private static int count=0;
private static int coins[]={1,2,5};
public static void main(String[] args) {
int amount=1*100;
int coinsCount[]=new int[coins.length];
//先每种至少一张
for(int i=coinsCount.length-1;i>=0;i--)
{
amount-=coins[i];
coinsCount[i]=1;
}
//从最大开始付
pay(amount,coinsCount.length-1,coinsCount);
System.out.println("count:"+count);
}

private static void print(int coinsCount[])
{
count++;
String str="";
int total=0;
for(int i=coinsCount.length-1;i>=0;i--)
{
if(i==coinsCount.length-1)
str+=coins[i]+"*"+coinsCount[i];
else
str+="+"+coins[i]+"*"+coinsCount[i];
total+=coins[i]*coinsCount[i];
}
System.out.println(str+"="+total);
}

private static void pay(int amount,int coinIdx,int srcCoinsCount[])
{
int coin=coins[coinIdx];
int[] coinsCount=new int[srcCoinsCount.length];
for(int i=0;i<coinsCount.length;i++)
{
coinsCount[i]=srcCoinsCount[i];
}
//如果是最小一种
if(coinIdx==0)
{
//整除,则合适
if(amount%coin==0)
{
coinsCount[coinIdx]+=amount/coin;
print(coinsCount);
}
return;
}
if(amount>=coin)
{
//用下一种面值付
pay(amount,coinIdx-1,coinsCount);
//继续用当前面值付
coinsCount[coinIdx]++;
amount-=coin;
pay(amount,coinIdx,coinsCount);
}
//不够,则用下一种面值付
else
{
pay(amount,coinIdx-1,coinsCount);
}

}
}
  • 打赏
  • 举报
回复
有点难度!
四维兜兜 2012-10-25
  • 打赏
  • 举报
回复
嗯,说的很在理,我在好好想想,再次感谢楼上的朋友
cstur4 2012-10-24
  • 打赏
  • 举报
回复
你再看看我的代码吧,你的做法很有问题。首先,每张都要用到你就没有考虑到。我的做法是判断没用的钞票是否大于等于余额,如果没有,我们显然不能再选当前的钞票了,要不然不可能满足每种至少一张的。另外,大于等于当前钞票的你考虑了,那么小于的呢?直接不管是不应该的吧?毕竟,我们要的是凑到刚好的总额。
cstur4 2012-10-24
  • 打赏
  • 举报
回复
额,你用递归了,我没看清。
cstur4 2012-10-24
  • 打赏
  • 举报
回复
你不是要递归?怎么又用for了。另外,你要看的是怎么做,别把重点放在用语言上。
四维兜兜 2012-10-23
  • 打赏
  • 举报
回复
代码有问题,排出来的分配方案有重复,求高手指点如何去重...另外感谢1楼提供的C++代码
import java.util.List;
import java.util.ArrayList;

public class TestOnly {

static int count = 0;

public static void main(String[] arg)
{
List<Integer> Cost = new ArrayList<Integer>();

// 用10块试试!
Pay(10, Cost);
System.out.println("种数=" + count);
}

static void Pay(int number, List<Integer> c)
{
int[] numbers = new int[3];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 5;


for (int i = 0; i < 3; i++)
{
if (number - numbers[i] == 0)
{
for (int j = 0; j < c.size(); j++)
{
System.out.print(c.get(j));
}
System.out.println(numbers[i]);
count++;
}
else if (number - numbers[i] > 0)
{
List<Integer> n = new ArrayList<Integer>();
n.addAll(c);
n.add(numbers[i]);
Pay(number - numbers[i], n);
}
}

}
}
四维兜兜 2012-10-23
  • 打赏
  • 举报
回复
我也知道答案是461,我用for循环做了,可是老师要我改成递归的,不会啊?求助啊!!
zly361814478 2012-10-23
  • 打赏
  • 举报
回复
看有没新的方法
soton_dolphin 2012-10-23
  • 打赏
  • 举报
回复
楼上的牛逼呀,顶
cstur4 2012-10-23
  • 打赏
  • 举报
回复
答案是461种。
cstur4 2012-10-23
  • 打赏
  • 举报
回复
C++代码哈

#include<iostream>
using namespace std;
#define LEN 3

int a[] = {1, 2, 5};
int remain(int i){
int total = 0;
for(int j=LEN-1;j>i;j--)
total += a[j];
return total;
}
long dfs(int total, int i){
if(i>=3){
if(total==0)
return 1;
return 0;
}

long res = 0;
for(int num=1;total-a[i]*num>=remain(i);++num){
res += dfs(total-num*a[i], i+1);
}

return res;
}
int main(){
printf("%ld\n", dfs(100, 0));

}

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧