这周Java课程有个小作业:Java递归实现从n个数中选取m个数的所有组合 代码如下: //其中 n 取 1,2,3,4,5 五个数, m 取 3 package javaText; public class text { static int N = 5; static int M = 3; static int[] a= new int[]{1,2,3,4,5}; static int[] b = new int[M]; public static void main(String[] ar…
import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; while (true) { System.out.print("请输入一个正整数(输入0退出循环):"); try { n = sc.nextInt(); } catch (Exception e) { System…
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus is possible for using animation. e.g. if n = 2 ; A→B ; A→C ; B→C; if n = 3; A→C ; A→B ; C→B ; A→C ; B→A ; B→C ; A→C; 翻译:模拟汉诺塔问题的移动规则:获得奖励的移动方法还是有可能的.…
欧几里得算法求最大公约数算法思想: 求p和q的最大公约数,如果q=0,最大公约数就是p:否则,p除以q余数为r,p和q的最大公约数即q和r的最大公约数. java实现代码: public class Demo0 { public static void main(String[] args) { System.out.println(gcd(24,120)); } public static int gcd(int p,int q){ if(q==0) return p; int r=p%q;…
递归生成一个如图的菜单,编写两个类数据模型Menu.和创建树形的MenuTree.通过以下过程实现: 1.首先从菜单数据中获取所有根节点. 2.为根节点建立次级子树并拼接上. 3.递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”. 首先,编写数据模型Menu.每条菜单有自己的id.父节点parentId.菜单名称text.菜单还拥有次级菜单children. import java.util.List; public class Menu { private String id;…
当初在开始接触Java时 学习File部分的一个小练习 挺有意思 一开始是通过看 北京圣思园 张龙老师的视频开始学校java的,必须强烈推荐,真很棒. 功能实现:主要实现以树形方式展现出该目录中的所有子目录和文件. 另外, 在展现的时候将目录排在上面,文件排在下面.每一层要加上缩进. 文件是jre6文件夹,我想这文件夹就不用我说什么了.换上自己的文件路径就可以了. import java.io.File; public class FileTest2 { public static int …
题目介绍: Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "b…
import java.io.File; /** * 文件综合使用示例 */ public class FileDelete { public static void main(String[] args) { File f = new File("d:\\test"); printAllFile(f); File f1 = new File("d:\\test"); deleteAll(f1); } /** * 打印f路径下所有的文件和文件夹 * * @param…