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"的项目旨在为MATLAB环境提供一个类来实现树的数据结构。下面将详细介绍树数据结构的基本概念、MATLAB中的类实现以及如何使用此类进行操作。 **树数据...
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 ...
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 ...
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...
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
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 ...
Thisarticlepresentsanewdesignofusingnonspeechaudio(i.e., earcons, spearcons, and short pulses) to represent distance and forward-direction for pedestrian navigation in eyes-free environ- ment....
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 ...
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 ...
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 ...
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...
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 ...
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 ...
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 ...
“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 ...
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 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.