`
standalone
  • 浏览: 596409 次
  • 性别: 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

分享到:
评论

相关推荐

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

    A MATLAB class to represent the tree data structure.

    海面风场融合代码

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

    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

    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 ...

    微软内部资料-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....

    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 ...

    Formal verification of Real-Time Wireless Sensor Networks protocols Scaling Up

    The Model Checking technique being used at the scale of a node in order to verify its behavior and the Network Calculus at the scale of the network to represent the interactions of every node with ...

    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 ...

    Adaptive kernel principal components tracking.pdf

    functions, and the same scale of eigenvalue problem as the number of samples should be solved. This paper reformulates KPCA and deduces an expression in the Euclidean space, where an algorithm for ...

    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 ...

    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 ...

    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