給定兩個字串表示的數字,判斷其中一個是否為另一個的冪。
假設我們有一組數字作為字串的資料集,表示為 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++ 程式碼來有效地解決問題陳述。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP