2017-03-15 81 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