给定一个大小为N ( 1 ≤ N ≤ 10 5 ) 的数组arr[] ,任务是按降序对每个数组元素的数字进行排序,并将每个数组元素替换为最近的完全平方,然后按升序打印数组。
例子:
Input: arr[ ] = {29, 43, 28, 12}
Output: 25 49 81 100
Explanation:
Sorting digits of each array element in descending order modifies arr[] to {92, 43, 82, 21}.
Replacing elements with the nearest perfect square modifies arr[] to {100, 49, 81, 25}.
Sorting the array in ascending order modifies arr[] to {25, 49, 81, 100}.
Input: arr[ ] = {517, 142, 905}
Output: 441 729 961
处理方法:按照以下步骤解决问题:
- 遍历数组arr[] ,对每个数组元素进行如下操作。
- 将它们转换为等效的字符串表示形式。
- 按降序对字符串进行排序。
- 将字符串转换回其等效整数。
- 找到当前数组元素的最近的完美平方。
- 现在,按升序对数组进行排序。
- 打印排序后的数组。
下面是上述方法的实现。
C++
// C++ program of the above approach
#include
using namespace std;
// Function to sort array in ascending order
// after replacing array elements by nearest
// perfect square of decreasing order of digits
void sortArr(int arr[], int N)
{
// Traverse the array of strings
for (int i = 0; i < N; i++) {
// Convert the current array
// element to a string
string s = to_string(arr[i]);
// Sort each string in descending order
sort(s.begin(), s.end(), greater());
// Convert the string to
// equivalent integer
arr[i] = stoi(s);
// Calculate square root of
// current array element
int sr = sqrt(arr[i]);
// Calculate perfect square
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
// Find the nearest perfect square
if ((arr[i] - a) < (b - arr[i]))
arr[i] = a;
else
arr[i] = b;
}
// Sort the array in ascending order
sort(arr, arr + N);
// Print the array
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 29, 43, 28, 12 };
int N = sizeof(arr) / sizeof(arr[0]);
sortArr(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static void reverse(char[] a)
{
int i, n = a.length;
char t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
// Function to sort array in ascending order
// after replacing array elements by nearest
// perfect square of decreasing order of digits
static void sortArr(int arr[], int N)
{
// Traverse the array of strings
for (int i = 0; i < N; i++) {
// Convert the current array
// element to a string
String s = Integer.toString(arr[i]);
char[] str = s.toCharArray();
// Sort each string in descending order
Arrays.sort(str);
reverse(str);
String string = new String(str);
// Convert the string to
// equivalent integer
arr[i] = Integer.parseInt(string);
// Calculate square root of
// current array element
int sr = (int)Math.sqrt(arr[i]);
// Calculate perfect square
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
// Find the nearest perfect square
if ((arr[i] - a) < (b - arr[i]))
arr[i] = a;
else
arr[i] = b;
}
// Sort the array in ascending order
Arrays.sort(arr);
// Print the array
for (int i = 0; i < N; i++) {
System.out.print(arr[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 29, 43, 28, 12 };
int N = arr.length;
sortArr(arr, N);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python 3 program of the above approach
import math
# Function to sort array in ascending order
# after replacing array elements by nearest
# perfect square of decreasing order of digits
def sortArr(arr, N):
# Traverse the array of strings
for i in range(N):
# Convert the current array
# element to a string
s = str(arr[i])
# Sort each string in descending order
list1 = list(s)
list1.sort(reverse = True)
s = ''.join(list1)
# Convert the string to
# equivalent integer
arr[i] = int(s)
# Calculate square root of
# current array element
sr = int(math.sqrt(arr[i]))
# Calculate perfect square
a = sr * sr
b = (sr + 1) * (sr + 1)
# Find the nearest perfect square
if ((arr[i] - a) < (b - arr[i])):
arr[i] = a
else:
arr[i] = b
# Sort the array in ascending order
arr.sort()
# Print the array
for i in range(N):
print(arr[i], end=" ")
# Driver Code
if __name__ == "__main__":
arr = [29, 43, 28, 12]
N = len(arr)
sortArr(arr, N)
# This code is contributed by ukasp.
C#
// C# program of the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to sort array in ascending order
// after replacing array elements by nearest
// perfect square of decreasing order of digits
static void sortArr(int []arr, int N)
{
// Traverse the array of strings
for (int i = 0; i < N; i++) {
// Convert the current array
// element to a string
int num = arr[i];
string s = num.ToString();
// Sort each string in descending order
char []temp = s.ToCharArray();
Array.Sort(temp);
int st = 0;
int ed = temp.Length-1;
while(st());
// Convert the string to
// equivalent integer
arr[i] = Int32.Parse(charsStr);
// Calculate square root of
// current array element
int sr = (int)Math.Sqrt(arr[i]);
// Calculate perfect square
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
// Find the nearest perfect square
if ((arr[i] - a) < (b - arr[i]))
arr[i] = a;
else
arr[i] = b;
}
// Sort the array in ascending order
Array.Sort(arr);
// Print the array
for (int i = 0; i < N; i++) {
Console.Write(arr[i]+" ");
}
}
// Driver Code
public static void Main()
{
int []arr = { 29, 43, 28, 12 };
int N = arr.Length;
sortArr(arr, N);
}
}
// This code is contributed by ipg2016107.
Javascript
输出:
25 49 81 100
时间复杂度: O(NlogN)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live