把100元钱换成1元,2元,5元,10元的有多少种换法?
麻烦大家都把你们的方式写下来我参考、参考? 
貌似可以用递归但是我没有写过,我写过四个嵌套循环,但是确实很不行那种方式。效率问题。
希望大虾们把你们的方式写出来给小弟我参考参考。

解决方案 »

  1.   

    循环嵌套可以稍微优化一下,比如for (int cnt10 = 0; cnt10 <= 10; cnt10++)另外,如果到某个循环,发现总和已经>100,那么就可以break了
      

  2.   

    这个google一下,应该有详细的答案
      

  3.   

    递归做比较好。。
    http://metaphy.javaeye.com/blog/162770
      

  4.   


    package com.keeya.test;//100元的各种零钱
    public class DecNum { public static void main(String[] args) {
    PrintHowToDivide(100);
    } public static void PrintHowToDivide(int value) {
    int temp = value;
    int ten = temp / 10;
    temp %= 10;
    int five = temp / 5;
    temp %= 5;
    int two = temp / 2;
    temp %= 2;
    int one = temp;
    PrintHowToDivide(value, ten, five, two, one);
    } /*
     * value为需要分解的钱,num_five, num_two, num_one 分别为当前的 5 2 1块的张数
     * 
     */
    private static void PrintHowToDivide(int value, int num_ten, int num_five, int num_two,
    int num_one) {
    System.out.println(value + "块钱有如下分法: " 
    + num_ten + " 张 10 元," +
    + num_five + " 张 5 元," 
    + num_two + " 张 2 元," + num_one + " 张 1 元");
    if (num_one == value)
    return;
    if (num_two > 0) {
    PrintHowToDivide(value,num_ten, num_five, num_two - 1, num_one + 2);
    } else if (num_five > 0) {
    PrintHowToDivide(value,num_ten, num_five - 1, (num_one + 5) / 2,
    (num_one + 5) % 2);
    } else if(num_ten > 0){
    PrintHowToDivide(value,num_ten - 1, (num_one + 10) / 5, (num_one + 10) % 5 / 2,
    (num_one + 10) % 5 % 2);
    }
    }
    }