给定一个整数数组arr [] ,任务是仅对那些在数组中的相对位置处是丑陋数字的元素(不影响其他元素的位置)进行排序。
丑陋的号码是号码的唯一素因子被2,3或5。
序列1、2、3、4、5、6、8、9、10、12、15 ….显示前几个丑陋的数字。按照惯例,其中包括1。
例子:
Input: arr[] = {13, 9, 11, 3, 2}
Output: 13 2 11 3 9
9, 3 and 2 are the only ugly numbers in the given array.
Input: arr[] = {1, 2, 3, 7, 12, 10}
Output: 1 2 3 7 10 12
方法:
- 开始遍历数组,对于每个元素arr [i] ,如果arr [i]是一个丑陋的数字,则将其存储在ArrayList中并更新arr [i] = -1
- 将所有丑陋的数字存储在ArrayList中之后,对更新的ArrayList进行排序。
- 再次遍历数组,对于每个元素,
- 如果arr [i] = -1,则从ArrayList中打印之前从未打印过的第一个元素。
- 否则,打印arr [i] 。
下面是上述方法的实现:
C++
// CPP implementation of the approach
#include
using namespace std;
// Function that returns true if n is an ugly number
bool isUgly(int n)
{
// While divisible by 2, keep dividing
while (n % 2 == 0)
n = n / 2;
// While divisible by 3, keep dividing
while (n % 3 == 0)
n = n / 3;
// While divisible by 5, keep dividing
while (n % 5 == 0)
n = n / 5;
// n must be 1 if it was ugly
if (n == 1)
return true;
return false;
}
// Function to sort ugly numbers
// in their relative positions
void sortUglyNumbers(int arr[], int n)
{
// To store the ugly numbers from the array
vector list;
int i;
for (i = 0; i < n; i++)
{
// If current element is an ugly number
if (isUgly(arr[i]))
{
// Add it to the ArrayList
// and set arr[i] to -1
list.push_back(arr[i]);
arr[i] = -1;
}
}
// Sort the ugly numbers
sort(list.begin(),list.end());
int j = 0;
for (i = 0; i < n; i++)
{
// Position of an ugly number
if (arr[i] == -1)
cout << list[j++] << " ";
else
cout << arr[i] << " ";
}
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 7, 12, 10 };
int n = sizeof(arr)/sizeof(arr[0]);
sortUglyNumbers(arr, n);
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation of the approach
import java.util.ArrayList;
import java.util.Collections;
class GFG {
// Function that returns true if n is an ugly number
static boolean isUgly(int n)
{
// While divisible by 2, keep dividing
while (n % 2 == 0)
n = n / 2;
// While divisible by 3, keep dividing
while (n % 3 == 0)
n = n / 3;
// While divisible by 5, keep dividing
while (n % 5 == 0)
n = n / 5;
// n must be 1 if it was ugly
if (n == 1)
return true;
return false;
}
// Function to sort ugly numbers
// in their relative positions
static void sortUglyNumbers(int arr[], int n)
{
// To store the ugly numbers from the array
ArrayList list = new ArrayList<>();
int i;
for (i = 0; i < n; i++) {
// If current element is an ugly number
if (isUgly(arr[i])) {
// Add it to the ArrayList
// and set arr[i] to -1
list.add(arr[i]);
arr[i] = -1;
}
}
// Sort the ugly numbers
Collections.sort(list);
int j = 0;
for (i = 0; i < n; i++) {
// Position of an ugly number
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[] = { 1, 2, 3, 7, 12, 10 };
int n = arr.length;
sortUglyNumbers(arr, n);
}
}
Python3
# Python3 implementation of the approach
# Function that returns true if n is an ugly number
def isUgly(n):
# While divisible by 2, keep dividing
while n % 2 == 0:
n = n // 2
# While divisible by 3, keep dividing
while n % 3 == 0:
n = n // 3
# While divisible by 5, keep dividing
while n % 5 == 0:
n = n // 5
# n must be 1 if it was ugly
if n == 1:
return True
return False
# Function to sort ugly numbers
# in their relative positions
def sortUglyNumbers(arr, n):
# To store the ugly numbers from the array
list = []
for i in range(0, n):
# If current element is an ugly number
if isUgly(arr[i]):
# Add it to the ArrayList
# and set arr[i] to -1
list.append(arr[i])
arr[i] = -1
# Sort the ugly numbers
list.sort()
j = 0
for i in range(0, n):
# Position of an ugly number
if arr[i] == -1:
print(list[j], end = " ")
j += 1
else:
print(arr[i], end = " ")
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 7, 12, 10]
n = len(arr)
sortUglyNumbers(arr, n)
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function that returns true
// if n is an ugly number
static bool isUgly(int n)
{
// While divisible by 2, keep dividing
while (n % 2 == 0)
n = n / 2;
// While divisible by 3, keep dividing
while (n % 3 == 0)
n = n / 3;
// While divisible by 5, keep dividing
while (n % 5 == 0)
n = n / 5;
// n must be 1 if it was ugly
if (n == 1)
return true;
return false;
}
// Function to sort ugly numbers
// in their relative positions
static void sortUglyNumbers(int []arr, int n)
{
// To store the ugly numbers from the array
List list = new List();
int i;
for (i = 0; i < n; i++)
{
// If current element is an ugly number
if (isUgly(arr[i]))
{
// Add it to the ArrayList
// and set arr[i] to -1
list.Add(arr[i]);
arr[i] = -1;
}
}
// Sort the ugly numbers
list.Sort();
int j = 0;
for (i = 0; i < n; i++)
{
// Position of an ugly number
if (arr[i] == -1)
Console.Write(list[j++] + " ");
else
Console.Write(arr[i] + " ");
}
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 7, 12, 10 };
int n = arr.Length;
sortUglyNumbers(arr, n);
}
}
// This code contributed by Rajput-Ji
输出:
1 2 3 7 10 12