JAVA这道题要如何用递归实现呢,求大神

最不会的就是递归了。。。。有人帮帮忙,提供一下递归的思路吗... 最不会的就是递归了。。。。有人帮帮忙,提供一下递归的思路吗 展开
 我来答
紫薇参星
科技发烧友

2016-05-06 · 有一些普通的科技小锦囊
知道大有可为答主
回答量:5983
采纳率:92%
帮助的人:3598万
展开全部

按照你的要求编写的Java递归程序如下:

import java.util.Scanner;

public class GGG {

 public static void main(String[] args) {

  int N = 0;

  Scanner sc=new Scanner(System.in);

  int num=sc.nextInt();

  for(int n=0;n<num;n++){

   N=sc.nextInt();

   int a[]=new int[N];

   for(int i=0;i<a.length;i++){

    a[i]=sc.nextInt();

   }

   System.out.print("case "+(n+1)+":");

   process(a,0);

   System.out.println();

  }

 }

 private static void process(int[] a, int n) {

  if(n==0){

   if(isPrime(a[n+1]))

    System.out.print(1+" ");

   else

    System.out.print(0+" ");

   

  }else if(n==a.length-1){

   if(isPrime(a[n-1]))

    System.out.print(1+" ");

   else

    System.out.print(0+" ");

   return;

  }else{

   if(isPrime(a[n-1])&&isPrime(a[n+1]))

    System.out.print(2+" ");

   else if(isPrime(a[n-1])||isPrime(a[n+1]))

    System.out.print(1+" ");

   else

    System.out.print(0+" ");

  }

  process(a,n+1);

 }

 public static boolean isPrime(int num) {

  int i;

  for(i=2;i<num;i++){

            if(num%i==0)

            break;

         }

       if(i==num){

        return true;

       }

       return false;

 }

}

运行结果:

2
5
5 7 2 9 13
case 1:1 2 1 2 0
3
10 4 5
case 2:0 1 0

mlymly2008
2016-05-06 · TA获得超过724个赞
知道小有建树答主
回答量:667
采纳率:100%
帮助的人:238万
展开全部
class Judge
{
public static int prim(int b[],int n)
{

int flag=1;
for(int i=2;i<=Math.sqrt(b[n]);i++)
{
if(b[n]%i==0)
{
flag=0;
}
}
return flag;
}
}

public class PrimGet {

public static void main(String[] args) {
int [] a={5,7,2,9,13};
//遍历出a[]中元素是否为素数
for(int i=0;i<a.length;i++)
{
System.out.print(Judge.prim(a, i)+" ");
}
System.out.println();
for(int i=0;i<a.length;i++)
{
if(i==0)
{
System.out.print(Judge.prim(a,i+1)+" ");
}else if(i==a.length-1)
{
System.out.print(Judge.prim(a,i-1));
}else
{
System.out.print(Judge.prim(a, i-1)+Judge.prim(a,i+1)+" ");
}
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
千锋教育
2016-05-06 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
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.
}
}
}
}
追问
没看懂。。。我复制到我的eclipse上找不到LetterInventory
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式