一道Java编程题,使用递归来做。

写一个叫做print的类函数,要求如下publicvoidprint(Stringphrase)Inthismethodyoushoulduserecursiveback... 写一个叫做print的类函数,要求如下
public void print(String phrase)
In this method you should use recursive backtracking to find and print all anagrams that can be formed using all of the
letters of the given phrase, in the same order and format as in the example log on the previous page. For example, if your
anagram solver is using the dictionary corresponding to dict1.txt and you are passed the phrase "hairbrush", your
method should produce the following output:
[bar, huh, sir]
[bar, sir, huh]
[briar, hush]
[huh, bar, sir]
[huh, sir, bar]
[hush, briar]
[sir, bar, huh]
[sir, huh, bar]
You should throw an IllegalArgumentException if the string is null. An empty string generates no output.

别介意是全英文的。。具体要求请下载看PDF文档:http://www.cs.washington.edu/education/courses/cse143/12wi/homework/6/spec.pdf
谢谢!
展开
 我来答
YoouoR
2012-05-25 · 超过13用户采纳过TA的回答
知道答主
回答量:59
采纳率:100%
帮助的人:26.7万
展开全部
import java.util.*; // in order to use the Set and Stack collections.
public class Anagrams {

private static String[] letters;

/**
* Anagrams, the constructor, initializes a new anagram solver over the given dictionary
* of words, and throws an IllegalArgumentException if the set passed is null.
* @param dictionary
*/
public Anagrams(Set<String> dictionary){
if(dictionary == null)
throw new IllegalArgumentException();
letters = dictionary.toArray(new String[0]);
}

/**
* getWords method returns a set containing all words from the dictionary that can be made
* using some or all of the letters in the given phrase, in alphabetical order, and throws
* an IllegalArgumentException if the set passed is null.
* @param phrase
* @return
*/
public Set<String> getWords(String phrase){
if(phrase == null)
throw new IllegalArgumentException();
Set<String> words = new TreeSet<String>();
LetterInventory phraseLetter = new LetterInventory(phrase);
for(String letter : letters){
if(phraseLetter.contains(letter)){
words.add(letter);
}
}
return words;
}

/**
* This first print method uses recursive backtracking to find and print all
* anagrams that can be formed using all of the letters of the given phrase,
* and throws an IllegalArgumentException if the set passed is null.
* @param phrase
*/
public void print(String phrase){
print(phrase, 0);
}

/**
* This second print method uses recursive backtracking to find and print all anagrams
* that can be formed using all of the letters of the given phrase and that include at
* most max words total, and throws an IllegalArgumentException if the set passed is null.
* @param phrase
* @param max
*/
public void print(String phrase, int max){
if(phrase == null || max < 0)
throw new IllegalArgumentException();
LetterInventory phraseLetter = new LetterInventory(phrase);
Stack<String> chosen = new Stack<String>();
String[] choices = getWords(phrase).toArray(new String[0]);
print(phraseLetter, chosen, choices, max);
}

/**
* This third print method is the basic recursive method of the two print methods above.
* @param phrase, the phrase that the user typed in.
* @param chosen, a collection of the words that are chosen to form the phrase using part
* of letters of the given phrase.
* @param choices, an String array that contains all the possible words that can be used
* to form the given phrase.
* @param max
*/
private static void print(LetterInventory phrase, Stack<String> chosen,String[] choices,int max){
if(phrase.isEmpty()){ //base case
System.out.println(chosen);
}else{ // recursive case
for(int i = 0; i<choices.length;i++){
if(phrase.contains(choices[i]) && (chosen.size() < max || max == 0)){
phrase.subtract(chosen.push(choices[i]));// choose
print(phrase, chosen, choices, max);// explore
phrase.add(chosen.pop());// backtrack.
}
}
}
}
}
砍出平成第一斧58
2016-01-01 · TA获得超过1567个赞
知道大有可为答主
回答量:2321
采纳率:86%
帮助的人:1200万
展开全部
public static int triangle(int n) {
if (n == 1) {
return 1;
} else {
return (n + triangle(n - 1));
}
}

程序调用自身的编程技巧称为递归( recursion)。递归做为一种算法在程序设计语言中广泛应用。 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。递归的能力在于用有限的语句来定义对象的无限集合。一般来说,递归需要有边界条件、递归前进段和递归返回段。当边界条件不满足时,递归前进;当边界条件满足时,递归返回。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
我是谁我在哪我要干什么呢
2012-05-24 · TA获得超过560个赞
知道小有建树答主
回答量:350
采纳率:100%
帮助的人:370万
展开全部
3个字符串进行全排列,用递归算法。下午要考试,不然可以写一写。下面是一个字符串全排列递归算法。你可以看一下。晚上回来写。

public class test{
static int count=0;
public static void main(String[] arg) throws ClassNotFoundException {
String s="1234";
long start=System.currentTimeMillis();
Pailie(s, " ");
System.out.println( "Total: "+count);
long end=System.currentTimeMillis();
System.out.println(end-start);
}
static void Pailie(String s,String p) {
if(s.length() <1) {
count++;
System.out.ptintln(p);
}
else {
int index[]=new int[s.length()];
for(int i=0;i<s.length();i++)
index[i]=s.indexOf(s.charAt(i));
for(int i=0; i <s.length(); i++) {
if(i==index[i])
Pailie(s.substring(1),p+s.substring(0,1));
s=s.substring(1)+s.substring(0,1);
}
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友845f74e61
2012-05-24 · TA获得超过6929个赞
知道大有可为答主
回答量:4050
采纳率:50%
帮助的人:1597万
展开全部
如下

----------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;

public class demo {

public static void main(String[] args) {
String[] array = { "bar", "huh", "sir" };
List<String> list = new ArrayList();
execute(array, list);
}

public static void execute(String[] array, List<String> list) {
for (int i = 0; i < array.length; i++) {
if (list.contains(array[i])) {
continue;
}
list.add(array[i]);
if (list.size() == array.length) {
String str = "";
for (int n = 0; n < list.size(); n++) {
str += list.get(n) + "\t";
}
System.out.println(str);
} else {
execute(array, list);
}
list.remove(list.size() - 1);
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
想知道为什么16
2015-12-23 · TA获得超过524个赞
知道小有建树答主
回答量:410
采纳率:0%
帮助的人:188万
展开全部
阶乘用递归实现!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式