java:递归:10元钱按1,2,5元任意组合
package game;
public class Money {
public static void main(String[] args) {
fun("", 10);
System.out.println("总共算法:" + i);
}
// 10元钱的组成,1,2,5任意组合
public static int i = 1;
public static void fun(String log, int n) {
// int num = n;
if (0 == n) {
System.out.println(log.substring(0, log.length() - 1) + "=");
return;
} else if (1 == n) {
System.out.println(log + "1" + "=");
return;
}
if (n >= 1)
fun(log + "1+", n - 1);
if (n >= 2)
fun(log + "2+", n - 2);
if (n >= 5)
fun(log + "5+", n - 5);
i++;
}
}
相关文档:
(一)线程同步
实现生产者消费者问题来说明线程问题,举例如下所示:
/**
* 生产者消费者问题
*/
public class ProducerConsumer {
/**
* 主方法
*/
public static void main(String[] args) {
ProductBox pb = new ProductBox ......
public class Split{
public static void main(String[] args){
double pai = 3.14159;
findTwo(pai);
public static void findTwo(double value){
System.out.println(new DecimalFormat("0.##"). ......
package arrays.myArray;
public class MyLinkedList {
private int size = 0;
private Node1 head = null;
// 添加
public void add(Object obj) {
add(size, obj);
}
// 修改
public void add(int index, Object obj) {
if (null == head) ......
package collection;
import java.util.*;
public class NewSet {
public static void main(String[] args) {
Set<Student> students = new HashSet<Student>();
for (int i = 0; i < 6; i++) {
students.add(new Student("Happy"+i,"male"+i,20+i)) ......
package collection;
public class Student {
public Student() {}
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
@Override
public String toString() {
......