`
standalone
  • 浏览: 613188 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Number of ways to represent money

阅读更多
Problem:

Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents.

My code:
package alg;
import java.util.Stack;

public class Coins {
    public final int coin[] = {25, 10, 5, 1};
    
    void printAllResult(int n){
        Stack<Integer> s = new Stack<Integer> ();
        printResult(n, s, 0);
    }
    
    void printResult(int n, Stack<Integer> s, int index){
        if(n == 0){
            System.out.println("Find one result:");
            for(Integer a : s){
                System.out.print("\t" + a);
            }
            System.out.println("\n");
            return;
        }
        for(int i=index;i<coin.length; i++){
            int v = coin[i];
            if(n>=v){
                s.push(v);
                printResult(n-v, s, i);
                s.pop();
            }
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Coins c = new Coins();
        c.printAllResult(26);
    }

}




Result:
Find one result:
	25	1

Find one result:
	10	10	5	1

Find one result:
	10	10	1	1	1	1	1	1

Find one result:
	10	5	5	5	1

Find one result:
	10	5	5	1	1	1	1	1	1

Find one result:
	10	5	1	1	1	1	1	1	1	1	1	1	1

Find one result:
	10	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1

Find one result:
	5	5	5	5	5	1

Find one result:
	5	5	5	5	1	1	1	1	1	1

Find one result:
	5	5	5	1	1	1	1	1	1	1	1	1	1	1

Find one result:
	5	5	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1

Find one result:
	5	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1

Find one result:
	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1	1

分享到:
评论

相关推荐

    海面风场融合代码

    做海面风场融合研究的代码 program var_blend ! use blend_data ! implicit none !======================================================= ... k represent the total number of QuikSCAT

    A MATLAB class to represent the tree data structure..zip

    这个"A MATLAB class to represent the tree data structure"的项目旨在为MATLAB环境提供一个类来实现树的数据结构。下面将详细介绍树数据结构的基本概念、MATLAB中的类实现以及如何使用此类进行操作。 **树数据...

    Learn to Program with Python

    Use Python's built-in data structures and packages to represent and make use of complex data from the Internet Who this book is for: This book assumes that you have absolutely no prior knowledge ...

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    Bits is the second of a series of 25 Chapters devoted to algorithms, problem solving, and C++ programming. This book is about low level bit programming Table of Contents Chapter 1. Given an unsigned ...

    《Prentice Hall Artificial Intelligence, A Modern Approach》

    Part III, "Knowledge and Reasoning," discusses ways to represent knowledge about the world—how it works, what it is currently like, what one's actions might do—and how to reason logically with that...

    Learn to Program with Python(Apress,2016)

    Get started in the world of software development: go from zero knowledge of programming ...Use Python’s built-in data structures and packages to represent and make use of complex data from the Internet

    微软内部资料-SQL性能优化2

    Paging is represented by a number of counters including page faults/sec, page input/sec and page output/sec. Page faults/sec include soft and hard page faults where as the page input/output counters ...

    A Comparative Study of Sonification Methods to Represent Distance and Forward-Direction in Pedestrian Navigation

    Thisarticlepresentsanewdesignofusingnonspeechaudio(i.e., earcons, spearcons, and short pulses) to represent distance and forward-direction for pedestrian navigation in eyes-free environ- ment....

    Greedy Layer-Wise Training

    computational elements required to represent some functions Deep multi layer neural networks have many levels of non linearities allowing them to compactly represent highly non linear and highly ...

    Ant Colony Optimization And Swarm Intelligence -2008.pdf

    more generally to the area of swarm intelligence. The rapid growth of the swarm intelligence field is attested by a number of indicators. First, the number of submissions and participants to the ANTS ...

    zigbee2007-协议英文

    The ZigBee specification has a number of options, which, if exercised in different ways by different vendors, will hamper both compliance testing activities and future product interoperability. The ...

    HART_HCF_SPEC-054-FSK物理层规范_A_8V0.PDF

    devices were created to represent their unique electrical properties. Devices Types: This revisions allows more device types to exist within a HART network. Device Topologies: This revision allows...

    Rational Numbers

    Use integer variables to represent the private instance variables of the class the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it ...

    D3-js-in-Action.pdf

    of ways. But it wasn’t until I started working with maps in grad school that I became aware of the immeasurable time and energy people have invested in understanding how to best represent data. I ...

    Probabilistic Robotics

    They key idea of probabilistic robotics is to represent uncertainty explicitly, using the calculus of probability theory. Put differently, instead of relying on a single “best guess” as to what ...

    GPU Pro 360 Guide to Geometry Manipulation pdf2018

    “Polygonal-Functional Hybrids for Computer Animation and Games,” by Denis Kravtsov et al., describes how to represent geometry with functions to overcome some of the challenges with polygons like ...

    IntroductiontoWirelessDigitalCommunication.epub

    I wrote this book to make the principles of wireless communication more accessible. Wireless communication is the dominant means of Internet access for most people, and it has become the means by ...

    Go CookBook golang

    Go provides excellent support for ...These recipes should lay the foundation for the use of interfaces to represent and modify data and should help you think about data in an abstract and flexible way.

Global site tag (gtag.js) - Google Analytics