给定一个大小为N的数组arr[]和一个整数K ,任务是打印数组的所有元素,这些元素可以表示为某个整数 (X) 的指数 K 的幂,即X K 。
例子:
Input: arr[] = {46656, 64, 256, 729, 16, 1000}, K = 6
Output: 46656 64 729
Explanation:
Only numbers 46656, 64, 729 can be expressed as a power of 6.
46656 = 66,
64 = 26,
729 = 36
Input: arr[] = {23, 81, 256, 125, 16, 1000}, K = 4
Output: 81 256 16
Explanation:
The number 81, 256, 16 can be expressed as a power of 4.
方法:解决上面提到的问题的主要思想是对数组中的每个数字,找到一个数字的第 N 个根。然后检查这个数字是否是整数。如果是,则打印它,否则跳到下一个数字。
下面是上述方法的实现:
CPP
// C++ implementation to print elements of
// the Array which can be expressed as
// power of some integer to given exponent K
#include
using namespace std;
#define ll long long
// Method returns Nth power of A
double nthRoot(ll A, ll N)
{
double xPre = 7;
// Smaller eps, denotes more accuracy
double eps = 1e-3;
// Initializing difference between two
// roots by INT_MAX
double delX = INT_MAX;
// x^K denotes current value of x
double xK;
// loop untill we reach desired accuracy
while (delX > eps) {
// calculating current value from previous
// value by newton's method
xK = ((N - 1.0) * xPre
+ (double)A / pow(xPre, N - 1))
/ (double)N;
delX = abs(xK - xPre);
xPre = xK;
}
return xK;
}
// Function to check
// whether its k root
// is an integer or not
bool check(ll no, int k)
{
double kth_root = nthRoot(no, k);
ll num = kth_root;
if (abs(num - kth_root) < 1e-4)
return true;
return false;
}
// Function to find the numbers
void printExpo(ll arr[], int n, int k)
{
for (int i = 0; i < n; i++) {
if (check(arr[i], k))
cout << arr[i] << " ";
}
}
// Driver code
int main()
{
int K = 6;
ll arr[] = { 46656, 64, 256,
729, 16, 1000 };
int n = sizeof(arr) / sizeof(arr[0]);
printExpo(arr, n, K);
return 0;
}
Java
// Java implementation to print elements of
// the Array which can be expressed as
// power of some integer to given exponent K
class GFG{
// Method returns Nth power of A
static double nthRoot(long A, long N)
{
double xPre = 7;
// Smaller eps, denotes more accuracy
double eps = 1e-3;
// Initializing difference between two
// roots by Integer.MAX_VALUE
double delX = Integer.MAX_VALUE;
// x^K denotes current value of x
double xK = 0;
// loop untill we reach desired accuracy
while (delX > eps) {
// calculating current value from previous
// value by newton's method
xK = ((N - 1.0) * xPre
+ (double)A / Math.pow(xPre, N - 1))
/ (double)N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
// Function to check
// whether its k root
// is an integer or not
static boolean check(long no, int k)
{
double kth_root = nthRoot(no, k);
long num = (long) kth_root;
if (Math.abs(num - kth_root) < 1e-4)
return true;
return false;
}
// Function to find the numbers
static void printExpo(long arr[], int n, int k)
{
for (int i = 0; i < n; i++) {
if (check(arr[i], k))
System.out.print(arr[i]+ " ");
}
}
// Driver code
public static void main(String[] args)
{
int K = 6;
long arr[] = { 46656, 64, 256,
729, 16, 1000 };
int n = arr.length;
printExpo(arr, n, K);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 implementation to prelements of
# the Array which can be expressed as
# power of some integer to given exponent K
# Method returns Nth power of A
def nthRoot(A, N):
xPre = 7
# Smaller eps, denotes more accuracy
eps = 1e-3
# Initializing difference between two
# roots by INT_MAX
delX = 10**9
# x^K denotes current value of x
xK = 0
# loop untiwe reach desired accuracy
while (delX > eps):
# calculating current value from previous
# value by newton's method
xK = ((N - 1.0) * xPre+ A /pow(xPre, N - 1))/ N
delX = abs(xK - xPre)
xPre = xK
return xK
# Function to check
# whether its k root
# is an integer or not
def check(no, k):
kth_root = nthRoot(no, k)
num = int(kth_root)
if (abs(num - kth_root) < 1e-4):
return True
return False
# Function to find the numbers
def printExpo(arr, n, k):
for i in range(n):
if (check(arr[i], k)):
print(arr[i],end=" ")
# Driver code
if __name__ == '__main__':
K = 6
arr = [46656, 64, 256,729, 16, 1000]
n = len(arr)
printExpo(arr, n, K)
# This code is contributed by mohit kumar 29
C#
// C# implementation to print elements of
// the Array which can be expressed as
// power of some integer to given exponent K
using System;
class GFG{
// Method returns Nth power of A
static double nthRoot(long A, long N)
{
double xPre = 7;
// Smaller eps, denotes more accuracy
double eps = 1e-3;
// Initializing difference between two
// roots by int.MaxValue
double delX = int.MaxValue;
// x^K denotes current value of x
double xK = 0;
// loop untill we reach desired accuracy
while (delX > eps) {
// calculating current value from previous
// value by newton's method
xK = ((N - 1.0) * xPre
+ (double)A / Math.Pow(xPre, N - 1))
/ (double)N;
delX = Math.Abs(xK - xPre);
xPre = xK;
}
return xK;
}
// Function to check
// whether its k root
// is an integer or not
static bool check(long no, int k)
{
double kth_root = nthRoot(no, k);
long num = (long) kth_root;
if (Math.Abs(num - kth_root) < 1e-4)
return true;
return false;
}
// Function to find the numbers
static void printExpo(long []arr, int n, int k)
{
for (int i = 0; i < n; i++) {
if (check(arr[i], k))
Console.Write(arr[i]+ " ");
}
}
// Driver code
public static void Main(String[] args)
{
int K = 6;
long []arr = { 46656, 64, 256,
729, 16, 1000 };
int n = arr.Length;
printExpo(arr, n, K);
}
}
// This code is contributed by Princi Singh
输出:
46656 64 729
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live