📜  比较给定 10 的两个幂

📅  最后修改于: 2022-05-13 01:56:06.661000             🧑  作者: Mango

比较给定 10 的两个幂

给定 4 个整数A、B、Z1Z2。任务是比较A*10 Z1B*10 Z2

例子:

天真的方法:将 A 与 Z1 零相乘,将 B 与 Z2 零相乘并比较两者但大数不能存储在超过 18 位的长整数中。
时间复杂度: O(1)
辅助空间: O(1)

有效方法:这个想法是比较 A 和 B 中的总位数,因为最大的位数比另一个最大。

  • 取两个变量adigitsbdigits并初始化为零
  • 将变量tempAtempB初始化为AB并在 while 循环中遍历并将位数存储在AB中。
  • 比较adigits+z1bdigits+z2 的值。如果它们的值相等,则执行以下任务:
    • 如果adigits大于bdigits,则将变量addZeros初始化为adigits-bdigits并将b初始化为10 addZeros ,反之亦然,如果bdigits大于adigits。
  • 现在,比较ab的值,执行结果。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to compare 2 numbers A and B
string CompareNumbers(int a, int b,
                      int z1, int z2)
{
 
    // Calculate number of digits
    // in both the numbers
    int Adigits = 0, Bdigits = 0;
    int tempA = a, tempB = b;
 
    while (tempA != 0) {
        Adigits++;
        tempA /= 10;
    }
    while (tempB != 0) {
        Bdigits++;
        tempB /= 10;
    }
 
    // Now compare both the digits with
    // adding zeroes
    if (Adigits + z1 > Bdigits + z2) {
        return ">";
    }
    else if (Adigits + z1 < Bdigits + z2) {
        return "<";
    }
 
    // If both condition are not true means
    // they have equal digits So now add zeroes
    // in smaller digit number to make equal
    // digits number as larger
 
    if (Adigits > Bdigits) {
        int addzeroes = Adigits - Bdigits;
        b *= pow(10, addzeroes);
    }
    else {
        int addzeroes = Bdigits - Adigits;
        a *= pow(10, addzeroes);
    }
    if (a == b) {
        return "=";
    }
    else if (a > b) {
        return ">";
    }
    else {
        return "<";
    }
}
 
// Driver Code
int main()
{
 
    int a = 20, z1 = 2;
    int b = 200, z2 = 1;
    string ans = CompareNumbers(a, b, z1, z2);
    cout << "A " << ans << " B";
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
  // Function to compare 2 numbers A and B
  static String CompareNumbers(int a, int b,
                               int z1, int z2) {
 
    // Calculate number of digits
    // in both the numbers
    int Adigits = 0, Bdigits = 0;
    int tempA = a, tempB = b;
 
    while (tempA != 0) {
      Adigits++;
      tempA /= 10;
    }
    while (tempB != 0) {
      Bdigits++;
      tempB /= 10;
    }
 
    // Now compare both the digits with
    // adding zeroes
    if (Adigits + z1 > Bdigits + z2) {
      return ">";
    } else if (Adigits + z1 < Bdigits + z2) {
      return "<";
    }
 
    // If both condition are not true means
    // they have equal digits So now add zeroes
    // in smaller digit number to make equal
    // digits number as larger
 
    if (Adigits > Bdigits) {
      int addzeroes = Adigits - Bdigits;
      b *= (int) Math.pow(10, addzeroes);
    } else {
      int addzeroes = Bdigits - Adigits;
      a *= (int) Math.pow(10, addzeroes);
    }
    if (a == b) {
      return "=";
    } else if (a > b) {
      return ">";
    } else {
      return "<";
    }
  }
 
  // Driver Code
  public static void main(String args[]) {
 
    int a = 20, z1 = 2;
    int b = 200, z2 = 1;
    String ans = CompareNumbers(a, b, z1, z2);
    System.out.println("A " + ans + " B");
  }
}
 
// This code is contributed by gfgking


Python3
# Python code for the above approach
 
# Function to compare 2 numbers A and B
def CompareNumbers(a, b, z1, z2):
 
    # Calculate number of digits
    # in both the numbers
    Adigits = 0
    Bdigits = 0
    tempA = a
    tempB = b
 
    while (tempA != 0):
        Adigits += 1
        tempA = tempA // 10
    while (tempB != 0):
        Bdigits += 1
        tempB = tempB // 10
 
    # Now compare both the digits with
    # adding zeroes
    if (Adigits + z1 > Bdigits + z2):
        return ">";
    elif (Adigits + z1 < Bdigits + z2):
        return "<";
 
    # If both condition are not true means
    # they have equal digits So now add zeroes
    # in smaller digit number to make equal
    # digits number as larger
    if (Adigits > Bdigits):
        addzeroes = Adigits - Bdigits;
        b *= (10 ** addzeroes)
    else:
        addzeroes = Bdigits - Adigits;
        a *= (10 ** addzeroes)
    if (a == b):
        return "=";
    elif (a > b):
        return ">";
    else:
        return "<";
 
# Driver Code
a = 20
z1 = 2;
b = 200
z2 = 1;
ans = CompareNumbers(a, b, z1, z2);
print("A " + ans + " B");
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG
{
   
// Function to compare 2 numbers A and B
static string CompareNumbers(int a, int b,
                      int z1, int z2)
{
 
    // Calculate number of digits
    // in both the numbers
    int Adigits = 0, Bdigits = 0;
    int tempA = a, tempB = b;
 
    while (tempA != 0) {
        Adigits++;
        tempA /= 10;
    }
    while (tempB != 0) {
        Bdigits++;
        tempB /= 10;
    }
 
    // Now compare both the digits with
    // adding zeroes
    if (Adigits + z1 > Bdigits + z2) {
        return ">";
    }
    else if (Adigits + z1 < Bdigits + z2) {
        return "<";
    }
 
    // If both condition are not true means
    // they have equal digits So now add zeroes
    // in smaller digit number to make equal
    // digits number as larger
 
    if (Adigits > Bdigits) {
        int addzeroes = Adigits - Bdigits;
        b *= (int)Math.Pow(10, addzeroes);
    }
    else {
        int addzeroes = Bdigits - Adigits;
        a *= (int)Math.Pow(10, addzeroes);
    }
    if (a == b) {
        return "=";
    }
    else if (a > b) {
        return ">";
    }
    else {
        return "<";
    }
}
 
// Driver Code
public static void Main()
{
 
    int a = 20, z1 = 2;
    int b = 200, z2 = 1;
    string ans = CompareNumbers(a, b, z1, z2);
    Console.Write("A " + ans + " B");
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
A = B

时间复杂度: O(1)
辅助空间: O(1)