2017-03-15 80 views
-3

下面的问题在我的一个考试中给出,并被要求仅使用Java解决以下问题。与Prime连接故障

问题是,我卡在程序应该返回给定的非负整数作为数组数组。任何人都可以提供解决方案吗?

在此先感谢。

如果其中一个条件成立,则说两个正数A和B被连接(由“A↔B”表示): (1)A和B具有相同的长度并且恰好在一个数字上不同; (2)在A(或B)的左边添加一位数字可以得到B(或A);例如,例如,23↔223和123↔23.

我们称一个素数p为2的相对如果存在2和P并且在链中不超过素之间连接质数的链P.

例如,127是2的亲戚。其中一个可能的链条如下所示: 2↔3↔13↔113↔103↔107↔127 但是,11和103不是2的亲戚。

设F(N)是不是2的亲属的素数之和≤N。 我们可以验证F(103)= 431和F(104)= 78728.

找到F(107)。

已编辑:我的部分 对不起,我不抄录我的解决方案,因为我没有给我结果。但只是这个问题的缘故,我觉得它应该返回非负数的部分,我有这样的事情 -

private static int[] toDigits(int n) { 
    if (n < 0) 
     throw new IllegalArgumentException(); 
    int[] temp = new int[10]; 
    int len = 0; 
    do { 
     temp[len] = n % 9; 
     n /= 9; 
     len++; 
    } while (n > 0); 
+1

这是粘贴在这里的项目欧拉问题 – jiltedpotato

+1

'谁能提供解决方案吗?'不,这不是这个网站的工作原理。显示你的努力,并详细说明你遇到的具体问题。 – tnw

+0

但我们应该回答这个问题吗? [链接](https://meta.stackoverflow.com/questions/307197/should-we-avoid-answering-questions-with-a-negative-score) – jiltedpotato

回答

-1
import java.util.Arrays; 
import java.util.PriorityQueue; 
import java.util.Queue; 


public final class infinitybyone implements TestSolution { 

    public static void main(String[] args) { 
     System.out.println(new infinitybyone().run()); 
    } 


    private static final int LIMIT = Library.pow(10, 7); 

    public String run() { 
     boolean[] isPrime = Library.listPrimality(LIMIT); 
     int[] pathMax = new int[isPrime.length]; 
     Arrays.fill(pathMax, Integer.MAX_VALUE); 


     Queue<IntPair> queue = new PriorityQueue<>(); 
     queue.add(new IntPair(2, 2)); 
     while (!queue.isEmpty()) { 
      IntPair item = queue.remove(); 
      int n = item.b; 
      int pmax = item.a; 
      if (pmax >= pathMax[n]) { 

       continue; 
      } 

      pathMax[n] = pmax; 

      int[] digits = toDigits(n); 
      int[] tempDigits = digits.clone(); 
      for (int i = 0; i < tempDigits.length; i++) { 
       for (int j = 0; j < 10; j++) { 
        tempDigits[i] = j; 
        int m = toNumber(tempDigits); 
        int nextPmax = Math.max(m, pmax); 
        if (m < isPrime.length && isPrime[m] && nextPmax < pathMax[m]) 
         queue.add(new IntPair(nextPmax, m)); 
       } 
       tempDigits[i] = digits[i]; 
      } 
     } 

     long sum = 0; 
     for (int i = 0; i < isPrime.length; i++) { 
      if (isPrime[i] && pathMax[i] > i) 
       sum += i; 
     } 
     return Long.toString(sum); 
    } 
    private static int[] toDigits(int n) { 
     if (n < 0) 
      throw new IllegalArgumentException(); 

     //************This is PROBABLY where you made the error************ 

     int[] temp = new int[11]; 
     int len = 0; 
     do { 
      temp[len] = n % 10; 
      n /= 10; 
      len++; 
     } while (n > 0); 

     int[] result = new int[len + 1]; 
     for (int i = 0; i < result.length; i++) 
      result[i] = temp[len - i]; 
     return result; 
    } 


    private static int toNumber(int[] digits) { 
     int result = 0; 
     for (int x : digits) 
      result = result * 10 + x; 
     return result; 
    } 



    private static class IntPair implements Comparable<IntPair> { 

     public final int a; 
     public final int b; 


     public IntPair(int a, int b) { 
      this.a = a; 
      this.b = b; 
     } 


     public int compareTo(IntPair other) { 
      return Integer.compare(a, other.a); 
     } 

    } 

} 

我真的不能告诉你哪里搞砸但至少从您分享的代码中,告诉我为什么要使用10而不是11?它应该在小端提取base-10