3个回答
展开全部
按照你的要求编写的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
展开全部
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.
}
}
}
}
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询