给定两个字符串表示的数字,判断其中一个是否为另一个的幂。


假设我们有一组数字作为字符串的数据集,表示为 str[]。现在的任务是将这两个表示为字符串的大数相乘。我们需要从这两个字符串表示的数字中找出,其中一个是否为另一个的幂。

这是一个过程的一般示例:

Input for the process us:
a = "374747", b = "52627712618930723"
Output : YES THE VALUE IS HERE
Explanation of the process:
374747^3 = 52627712618930723
Input for the process is :
a = "2", b = "4099"
Output : NO THE VALUE IS NOT HERE

从两个字符串表示的数字中查找一个数是否为另一个数的幂的算法

在这个可能的算法中,我们将执行以下操作:在 C++ 环境中,从给定的两个字符串表示的数字中检查一个数字是否为另一个数字的幂。通过此算法,我们将构建一些 C++ 语法,以便以有效的方式了解问题陈述。

  • 步骤 1 - 开始该过程。

  • 步骤 2 - 声明输入输出流。

  • 步骤 3 - 导入内置类和声明的函数。

  • 步骤 4 - 声明一个字符串函数来查找值。

  • 步骤 5 - 如果第一个字符串大于第二个字符串,则返回 true。

  • 步骤 6 - 为乘积字符串声明一个值。

  • 步骤 7 - 将数字相乘。

  • 步骤 8 - 声明一个 for 循环来迭代该方法。

  • 步骤 9 - 如果数字超过 9 的值,则将累积的进位加到前一个数字。

  • 步骤 10 - 如果所有值都为零,则返回值零。

  • 步骤 11 - 开始删除零的值。

  • 步骤 12 - 删除多余的零。

  • 步骤 13 - 删除前导零的值。

  • 步骤 14 - 返回幂的值。

  • 步骤 15 - 声明字符串一和二,并定义值是否存在。

  • 步骤 16 - 返回值并终止该过程。

从两个字符串表示的数字中查找一个数是否为另一个数的幂的语法

int num;
   cout << "ASKING FOR THE INPUT STATEMENT: ";
   cin >> num;
   if((num != 0) && ((num &(num - 1)) == 0))
      cout << num << " IS A POWER OF THE NUMBER." << endl;
   else
      cout << num << " NOT A POWER OF THE NUMBER." << endl;
int checkPowerofTwo(int x){
   if (x == 0)
   return 0;
   while( x != 1){
      if(x % 2 != 0)
      return 0;
      x /= 2;
   }
return 1;

在这个可能的语法中,我们将执行以下操作:在 C++ 环境中,从给定的两个字符串表示的数字中检查一个数字是否为另一个数字的幂。通过这些可能的语法,我们将构建一些 C++ 示例代码,以更广泛的方式解释问题陈述。

遵循的方法

  • 方法 1 - 使用字符串乘法 (string multiply(string one, string two))、isPower()、对数函数和比较方法的 C++ 程序来检查一个数字是否为另一个数字的幂

  • 方法 2 - 使用带循环更改的二分查找、CheckPowerofNumber() 和遍历方法的 C++ 程序来检查一个数字是否为另一个数字的幂

方法 1:使用字符串乘法 (string Multiply(string one, String two))、IsPower()、对数函数和比较方法

字符串乘法 (string Multiply(string one, String two)) 方法的使用

在这个可能的方法中,我们将应用使用字符串乘法 (string multiply(string one, string two)) 方法来查找从给定的两个字符串表示的数字中,其中一个是否为另一个的幂。

last = powers[0] = y;
for (i = 1; last < x; i++){
   last *= last;
   powers[i] = last;
}
while (x >= y) {
	unsigned long top = powers[--i];
	if (x >= top) {
		unsigned long x1 = x / top;
		if (x1 * top != x) return 0;
		x = x1;
	}
}
return (x == 1);
sort(A, A+n);
for (int i=0; i<n; i++){
	for (int j=i+1; j<n; j++){
		int x = 0;
		while ((A[i]*pow(k, x)) <= A[j]){
			if ((A[i]*pow(k, x)) == A[j]){
				ans++;
				break;
			}
			x++;
		}
	}
}
return ans;

示例

//C++ program to check if one number is power of other by using string multiply(string one, string two)
#include <bits/stdc++.h>
using namespace std;
bool isGreaterThanEqualTo(string s1, string s2){
   if (s1.size() > s2.size())
   return true;
   return (s1 == s2);
}
string multiply(string s1, string s2){
   int n = s1.size();
   int m = s2.size();
   vector<int> result(n + m, 0);
   for (int i = n - 1; i >= 0; i--)
   for (int j = m - 1; j >= 0; j--)
   result[i + j + 1] +=
   (s1[i] - '0') * (s2[j] - '0');
   int size = result.size();
   for (int i = size - 1; i > 0; i--){
      if (result[i] >= 10) {
         result[i - 1] += result[i] / 10;
         result[i] = result[i] % 10;
      }
   }
   int i = 0;
   while (i < size && result[i] == 0)
   i++;
   if (i == size)
   return "0";
   string temp;
   while (i < size){
      temp += (result[i] + '0');
      i++;
   }
   return temp;
}
string removeLeadingZeores(string s){
   int n = s.size();
   int i = 0;
   while (i < n && s[i] == '0')
   i++;
   if (i == n)
   return "0";
   string temp;
   while (i < n)
   temp += s[i++];
   return temp;
}
bool isPower(string s1, string s2){
   s1 = removeLeadingZeores(s1);
   s2 = removeLeadingZeores(s2);
   if (s1 == "0" || s2 == "0")
   return false;
   if (s1 == "1" && s2 == "1")
   return true;
   if (s1 == "1" || s2 == "1")
   return true;
   if (s1.size() > s2.size())
   return isPower(s2, s1);
   string temp = s1;
   while (!isGreaterThanEqualTo(s1, s2))
   s1 = multiply(s1, temp);
   return s1 == s2;
}
int main(){
   string s1 = "1610", s2 = "52627712618930723";
   cout << (isPower(s1, s2) ? "YES\n" : "NO\n");
   s1 = "19972001", s2 = "2022";
   cout << (isPower(s1, s2) ? "YES\n" : "NO\n");
   return 0;
}

输出

NO
NO

IsPower() 方法的使用

在这个可能的方法中,我们将应用 isPower() 方法来查找从给定的两个字符串表示的数字中,其中一个是否为另一个的幂。

示例

//C++ program to check if one number is power of other by using the isPower() method
#include <bits/stdc++.h>
using namespace std;
bool isPower(int x, long int y){
   if (x == 1)
   return (y == 1);
   long int pow = 1;
   while (pow < y)
   pow *= x;
   return (pow == y);
}
int main(){
   cout << isPower(16, 07) << endl;
   cout << isPower(10, 1) << endl;
   cout << isPower(97, 10) << endl;
   cout << isPower(2, 30) << endl;
   return 0;
}

输出

0
1
0
0

对数函数和比较方法的使用

在这个可能的方法中,我们将应用对数函数来计算值,并使用比较方法来查找从给定的两个字符串表示的数字中,其中一个是否为另一个的幂。

示例

//C++ program to check if one number is power of other by using the logarithm function to calculate value and use the compare method also
#include <iostream>
#include <math.h>
using namespace std;
bool isPower(int x, int y){
   double res1 = log(y) / log(x);
   double res2 = log(y) / log(x);
   return (res1 == res2);
}
int main(){
   cout << isPower(16, 07) << endl;
   return 0;
}

输出

1

方法 2:使用带循环更改的二分查找、CheckPowerofNumber() 和遍历方法

带循环更改的二分查找方法的使用

在这个可能的方法中,我们将应用带循环链的二叉搜索树方法来查找从给定的两个字符串表示的数字中,其中一个是否为另一个的幂。

double d = Math.log(Math.abs(x)) / Math.log(Math.abs(y));
if ((x > 0 && y > 0) || (x < 0 && y < 0)) {
if (d == (int) d) {
   return true;
} else {
   return false;
}
} else if (x > 0 && y < 0) {
   if ((int) d % 2 == 0) {
      return true;
   } else {
      return false;
   }
} else {
return false;

示例

//C++ program to check if one number is power of other by using the loop changing exponent with binary search
#include <iostream>
#include <cmath>
using namespace std;
bool isIntegerPower(int x, int y){
   int low = 0, high;
   int exp = 1;
   int val = y;
   while(1){
      val = pow((double)y, exp);
      if(val == x)
      return true;
      else if(val > x)
      break;
      low = exp;
      exp = exp * 2;
      high = exp;
   }
   int mid = (low + high)/2;
   while(low < high){
      val = pow((double)y, mid);
      if(val > x){
         high = mid-1;
      } else if(val == x){
         return true;
      } else if(val < x){
         low = mid+1;
      }
      mid = (low + high)/2;
   }
   return false;
}
int main(){
   cout<<isIntegerPower(1607,2);
}

输出

0

CheckPowerofNumber() 方法的使用

在这个可能的方法中,我们将应用检查数字幂的方法来查找从给定的两个字符串表示的数字中,其中一个是否为另一个的幂。

示例

//C++ program to check if one number is power of other by using the function is checkPowerofTwo() or not
#include <iostream>
#include <cmath>
using namespace std;
int checkPowerofTwo(int n);
int main(){
   int num;
   printf("Enter the number you want to test today: ");
   scanf("%d", &num);
   if (checkPowerofTwo(num) == 1)
   printf("\n%d - this number is a power of 2\n", num);
   else
   printf("\n%d - this number is not a power of 2\n", num);
   return 0;
}
int checkPowerofTwo(int x){
   if (x == 0)
   return 0;
   while( x != 1){
      if(x % 2 != 0)
      return 0;
      x /= 2;
   }
   return 1;
}

输出

Enter the number you want to test today: 1607
1607 - this number is not a power of 2
Enter the number you want to test today: 64
64 - this number is a power of 2

遍历方法的使用

在这个可能的方法中,我们将部署遍历方法来查找从给定的两个字符串表示的数字中,其中一个是否为另一个的幂。

示例

//C++ program to check if one number is power of other by using traversing method
#include <bits/stdc++.h>
using namespace std;
int countPairs(int A[], int n, int k){
   int ans = 0;
   sort(A, A + n);
   for (int i = 0; i < n; i++){
      for (int j = i + 1; j < n; j++){
         int x = 0;
         while ((A[i] * pow(k, x)) <= A[j]){
            if ((A[i] * pow(k, x)) == A[j]){
               ans++;
               break;
            }
            x++;
         }
      }
   }
   return ans;
}
int main() {
   int A[] = {3, 8, 9, 12, 16, 10,7, 1997, 2001, 18, 4, 24, 2, 6};
   int n = sizeof(A) / sizeof(A[0]);
   int k = 3;
   cout << countPairs(A, n, k);
   return 0;
}

输出

6

结论

今天在这篇文章中,我们学习了如何在 C++ 环境中实现构建和应用各种方法的过程,以检查从给定的两个字符串表示的数字中,其中一个数字是否为另一个数字的幂。通过上述逻辑、语法和算法,我们尝试构建一些 C++ 代码来有效地解决问题陈述。

更新时间: 2023 年 12 月 27 日

164 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.