📌  相关文章
📜  按相对位置对所有特殊素数进行排序

📅  最后修改于: 2021-05-17 06:40:43             🧑  作者: Mango

给定一个大小为N的正整数的数组arr [] ,任务是将所有特殊素数按其相对位置排序(不影响其他元素的位置)。特殊质数是质数,可以表示为其他两个质数之和。

例子:

方法:

  • 开始遍历数组,对于每个元素arr [i] ,如果arr [i]是一个特殊的质数,则将其存储在向量中并更新arr [i] = -1
  • 将所有特殊质数存储在列表中之后,对更新后的列表进行排序。
  • 再次遍历数组,对于每个元素,
    • 如果arr [i] = -1,则打印之前未打印过的列表中的第一个元素。
    • 否则,打印arr [i]

下面是上述方法的实现:

C++
// C++ implementation of the approach 
#include  
using namespace std; 
  
// Function for the Sieve of Eratosthenes 
void sieveOfEratosthenes(bool prime[], int n) 
{ 
    prime[0] = prime[1] = false; 
    for (int p = 2; p * p <= n; p++) { 
  
        // If prime[p] is not changed, then it is a prime 
        if (prime[p] == true) { 
  
            // Update all multiples of p greater than or 
            // equal to the square of it 
            // numbers which are multiple of p and are 
            // less than p^2 are already been marked. 
            for (int i = p * p; i <= n; i += p) 
                prime[i] = false; 
        } 
    } 
} 
  
// Function to sort the special primes 
// in their relative positions 
void sortSpecialPrimes(int arr[], int n) 
{ 
  
    // Maximum element from the array 
    int maxVal = *max_element(arr, arr + n); 
  
    // prime[i] will be true if i is a prime 
    bool prime[maxVal + 1]; 
    memset(prime, true, sizeof(prime)); 
    sieveOfEratosthenes(prime, maxVal); 
  
    // To store the special primes 
    // from the array 
    vector list; 
  
    for (int i = 0; i < n; i++) { 
  
        // If current element is a special prime 
        if (prime[arr[i]] && prime[arr[i] - 2]) { 
  
            // Add it to the ArrayList 
            // and set arr[i] to -1 
            list.push_back(arr[i]); 
            arr[i] = -1; 
        } 
    } 
  
    // Sort the special primes 
    sort(list.begin(), list.end()); 
  
    int j = 0; 
    for (int i = 0; i < n; i++) { 
  
        // Position of a special prime 
        if (arr[i] == -1) 
            cout << list[j++] << " "; 
        else
            cout << arr[i] << " "; 
    } 
} 
  
// Driver code 
int main() 
{ 
    int arr[] = { 31, 5, 2, 1, 7 }; 
    int n = sizeof(arr) / sizeof(int); 
  
    sortSpecialPrimes(arr, n); 
  
    return 0; 
}


Java
// Java implementation of the above approach 
import java.util.*; 
  
class GFG{ 
  
// Function for the Sieve of Eratosthenes 
static void sieveOfEratosthenes(boolean prime[], 
                                int n) 
{ 
    prime[0] = prime[1] = false; 
    for(int p = 2; p * p <= n; p++) 
    { 
  
        // If prime[p] is not changed, 
        // then it is a prime 
        if (prime[p] == true) 
        { 
  
            // Update all multiples of p 
            // greater than or equal to the 
            // square of it numbers which are 
            // multiple of p and are less than 
            // p^2 are already been marked. 
            for(int i = p * p; i <= n; i += p) 
                prime[i] = false; 
        } 
    } 
} 
  
// Function to sort the special primes 
// in their relative positions 
static void sortSpecialPrimes(int arr[], int n) 
{ 
      
    // Maximum element from the array 
    int maxVal = Arrays.stream(arr).max().getAsInt(); 
  
    // prime[i] will be true if i is a prime 
    boolean []prime = new boolean[maxVal + 1]; 
    for(int i = 0; i < prime.length; i++) 
        prime[i] = true; 
          
    sieveOfEratosthenes(prime, maxVal); 
  
    // To store the special primes 
    // from the array 
    Vector list = new Vector(); 
  
    for(int i = 0; i < n; i++) 
    { 
          
        // If current element is a special prime 
        if (prime[arr[i]] && prime[arr[i] - 2]) 
        { 
              
            // Add it to the ArrayList 
            // and set arr[i] to -1 
            list.add(arr[i]); 
            arr[i] = -1; 
        } 
    } 
  
    // Sort the special primes 
    Collections.sort(list); 
  
    int j = 0; 
    for(int i = 0; i < n; i++) 
    { 
          
        // Position of a special prime 
        if (arr[i] == -1) 
            System.out.print(list.get(j++) + " "); 
        else
            System.out.print(arr[i] + " "); 
    } 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    int arr[] = { 31, 5, 2, 1, 7 }; 
    int n = arr.length; 
  
    sortSpecialPrimes(arr, n); 
} 
} 
  
// This code is contributed by PrinciRaj1992


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
using System.Linq;
  
class GFG{
  
// Function for the Sieve of Eratosthenes
static void sieveOfEratosthenes(bool []prime,
                                int n)
{
    prime[0] = prime[1] = false;
    for(int p = 2; p * p <= n; p++)
    {
          
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true)
        {
              
            // Update all multiples of p
            // greater than or equal to the 
            // square of it numbers which are
            // multiple of p and are less than
            // p^2 are already been marked.
            for(int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to sort the special primes
// in their relative positions
static void sortSpecialPrimes(int []arr, int n)
{
      
    // Maximum element from the array
    int maxVal = arr.Max();
  
    // prime[i] will be true if i is a prime
    bool []prime = new bool[maxVal + 1];
    for(int i = 0; i < prime.Length; i++)
        prime[i] = true;
          
    sieveOfEratosthenes(prime, maxVal);
  
    // To store the special primes
    // from the array
    List list = new List();
  
    for(int i = 0; i < n; i++) 
    {
          
        // If current element is a special prime
        if (prime[arr[i]] && prime[arr[i] - 2]) 
        {
              
            // Add it to the List
            // and set arr[i] to -1
            list.Add(arr[i]);
            arr[i] = -1;
        }
    }
  
    // Sort the special primes
    list.Sort();
  
    int j = 0;
    for(int i = 0; i < n; i++)
    {
          
        // Position of a special prime
        if (arr[i] == -1)
            Console.Write(list[j++] + " ");
        else
            Console.Write(arr[i] + " ");
    }
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 31, 5, 2, 1, 7 };
    int n = arr.Length;
  
    sortSpecialPrimes(arr, n);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
5 7 2 1 31