📌  相关文章
📜  重新排列数组中x的倍数的所有元素,并按升序排列

📅  最后修改于: 2021-04-29 15:19:50             🧑  作者: Mango

给定一个整数数组“ arr”和一个数字x,任务是按升序将所有数组x的元素按其相对位置升序排序,即,其他元素的其他位置不得受到影响。

例子

方法:

  1. 遍历数组,并检查数字是否为x的倍数。如果是,请将其存储在向量中。
  2. 然后,按升序对向量进行排序。
  3. 再次遍历数组,并用矢量元素一一替换5的倍数的元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
  
#include 
using namespace std;
  
// Function to sort all the
// multiples of x from the
// array in ascending order
void sortMultiples(int arr[], int n, int x)
{
    vector v;
  
    // Insert all multiples of 5 to a vector
    for (int i = 0; i < n; i++)
        if (arr[i] % x == 0)
            v.push_back(arr[i]);
  
    // Sort the vector
    sort(v.begin(), v.end());
  
    int j = 0;
  
    // update the array elements
    for (int i = 0; i < n; i++) {
        if (arr[i] % x == 0)
            arr[i] = v[j++];
    }
}
  
// Driver code
int main()
{
    int arr[] = { 125, 3, 15, 6, 100, 5 };
    int x = 5;  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    sortMultiples(arr, n, x);
  
    // Print the result
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
  
    return 0;
}


Java
import java.util.Collections;
import java.util.Vector;
  
// Java implementation of the approach 
class GFG {
  
// Function to sort all the 
// multiples of x from the 
// array in ascending order 
    static void sortMultiples(int arr[], int n, int x) {
        Vector v = new Vector();
  
        // Insert all multiples of 5 to a vector 
        for (int i = 0; i < n; i++) {
            if (arr[i] % x == 0) {
                v.add(arr[i]);
            }
        }
  
        // Sort the vector 
        Collections.sort(v);
        //sort(v.begin(), v.end()); 
  
        int j = 0;
  
        // update the array elements 
        for (int i = 0; i < n; i++) {
            if (arr[i] % x == 0) {
                arr[i] = v.get(j++);
            }
        }
    }
  
// Driver code 
    public static void main(String[] args) {
        int arr[] = {125, 3, 15, 6, 100, 5};
        int x = 5;
        int n = arr.length;
  
        sortMultiples(arr, n, x);
  
        // Print the result 
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
// This code is contributed by Rajput-Ji


Python3
# Python 3 implementation of the approach
  
# Function to sort all the multiples of x 
# from the array in ascending order
def sortMultiples(arr, n, x):
    v = []
  
    # Insert all multiples of 5 to a vector
    for i in range(0, n, 1):
        if (arr[i] % x == 0):
            v.append(arr[i])
  
    # Sort the vector
    v.sort(reverse=False)
  
    j = 0
  
    # update the array elements
    for i in range(0, n, 1):
        if (arr[i] % x == 0):
            arr[i] = v[j]
            j += 1
  
# Driver code
if __name__ == '__main__':
    arr = [ 125, 3, 15, 6, 100, 5] 
    x = 5
    n = len(arr)
  
    sortMultiples(arr, n, x)
  
    # Print the result
    for i in range(0, n, 1):
        print(arr[i], end = " ")
  
# This code is contributed by 
# Surendra _Gangwar


C#
// C# implementation of the approach
using System; 
using System.Collections.Generic; 
  
class GFG 
{ 
    // Function to sort all the
    // multiples of x from the
    // array in ascending order
    static void sortMultiples(int []arr, 
                            int n, int x)
    {
        List v = new List();
        int i;
          
        // Insert all multiples of 5 to a vector
        for (i = 0; i < n; i++)
        if (arr[i] % x == 0)
            v.Add(arr[i]); 
              
        // Sort the vector
        v.Sort();
        int j = 0;
  
        // update the array elements
        for (i = 0; i < n; i++)
        {
            if (arr[i] % x == 0)
                arr[i] = v[j++];
        }
    }
  
    // Driver code
    public static void Main() 
    { 
        int []arr = {125, 3, 15, 6, 100, 5}; 
        int x = 5;
        int n = arr.Length; 
        sortMultiples(arr, n, x); 
      
        // Print the result
        for (int i = 0; i < n; i++) 
        { 
            Console.Write(arr[i] + " "); 
        } 
    }
}
  
// This code is cntributed by
// Shivi_Aggarwal


输出:
5 3 15 6 100 125