📌  相关文章
📜  表示两个给定数字的乘积的没有任何乘号的最小字符串

📅  最后修改于: 2021-09-06 05:28:52             🧑  作者: Mango

给定两个数字AB ,任务是打印长度最小的字符串,该字符串的计算结果为两个给定数字的乘积,即A*B ,不使用乘号。

例子:

方法:想法是使用左移运算符来查找产品。以下是步骤:

  • 将 B 表示为 2 的幂。
  • 因此,A和B的乘积可以写为
  • 使用“<<” (左移运算符)将一个数乘以 2 的任意幂。
  • 因此 A x B = A << k 1 + A << k 2 + … + A << k n
  • 为了找到k i,我们使用 log()函数并用余数B – 2 k i继续该过程,直到余数变为0或余数的对数变为 0。
  • 类似地,通过将A表示为 2 的幂来表示 A*B = B<< k 1 + B<< k 2 + … + B<< k n
  • 比较两种表示并打印长度较小的字符串。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include 
using namespace std;
 
// Function to find the string
// which evaluates to the
// product of A and B
string len(long A, long B)
{
  // Stores the result
  string res = "";
  long Log = 0;
 
  do
  {   
    // 2 ^ log <= B &&
    // 2 ^ (log + 1) > B
    Log = (long)(log(B) / log(2));
 
    // Update res to
    // res+=A X 2^log
    if (Log != 0)
    {
      res = res + to_string(A) +
            "<<" + to_string(Log);
    }
    else
    {
      // Update res to
      // res+=A X 2^0
      res += A;
      break;
    }
 
    // Find the remainder
    B = B - (long)pow(2, Log);
 
    // If remainder is
    // not equal to 0
    if (B != 0)
    {
      res += "+";
    }
    else
      break;
  } while (Log != 0);
 
  // Return the
  // resultant string
  return res;
}
 
// Function to find the minimum
// length representation of A*B
void minimumString(long A, long B)
{
  // Find representation of form
  // A << k1 + A << k2 + ... + A << kn
  string res1 = len(A, B);
 
  // Find representation of form
  // B << k1 + B << k2 + ... + B << kn
  string res2 = len(B, A);
 
  // Compare the length of
  // the representations
  if (res1.length() > res2.length())
  {
    cout << res2 << endl;
  }
  else
  {
    cout << res1 << endl;
  }
}
   
// Driver code
int main()
{
  // Product A X B
  long A = 6;
  long B = 10;
 
  // Function Call
  minimumString(A, B);
 
  return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program for the above approach
 
class GFG {
 
    // Function to find the string
    // which evaluates to the
    // product of A and B
    public static String len(long A,
long B)
    {
        // Stores the result
        String res = "";
        long log = 0;
 
        do {
 
            // 2 ^ log <= B &&
            // 2 ^ (log + 1) > B
            log = (long)(Math.log(B)
                         / Math.log(2));
 
            // Update res to
            // res+=A X 2^log
            if (log != 0) {
 
                res += A + "<<" + log;
            }
            else {
 
                // Update res to res+=A X 2^0
                res += A;
                break;
            }
 
            // Find the remainder
            B = B - (long)Math.pow(2, log);
 
            // If remainder is not equal
            // to 0
            if (B != 0) {
                res += "+";
            }
            else
                break;
        } while (log != 0);
 
        // Return the resultant string
        return res;
    }
 
    // Function to find the minimum
    // length representation of A*B
    public static void
    minimumString(long A, long B)
    {
        // Find representation of form
        // A << k1 + A << k2 + ... + A << kn
        String res1 = len(A, B);
 
        // Find representation of form
        // B << k1 + B << k2 + ... + B << kn
        String res2 = len(B, A);
 
        // Compare the length of
        // the representations
        if (res1.length() > res2.length()) {
            System.out.println(res2);
        }
        else {
            System.out.println(res1);
        }
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Product A X B
        long A = 6;
        long B = 10;
 
        // Function Call
        minimumString(A, B);
    }
}


Python3
# Python3 program for the above approach
from math import log
 
# Function to find the string
# which evaluates to the
# product of A and B
def lenn(A, B):
     
    # Stores the result
    res = ""
    logg = 0
 
    while True:
 
        # 2 ^ logg <= B &&
        # 2 ^ (logg + 1) > B
        logg =log(B) // log(2)
 
        # Update res to
        # res+=A X 2^logg
        if (logg != 0):
            res += (str(A) + "<<" +
                    str(int(logg)))
        else:
 
            # Update res to res+=A X 2^0
            res += A
            break
 
        # Find the remainder
        B = B - pow(2, logg)
 
        # If remainder is not equal
        # to 0
        if (B != 0):
            res += "+"
        else:
            break
             
        if logg == 0:
            break
 
    # Return the resultant string
    return res
 
# Function to find the minimum
# length representation of A*B
def minimumString(A, B):
     
    # Find representation of form
    # A << k1 + A << k2 + ... + A << kn
    res1 = lenn(A, B)
 
    # Find representation of form
    # B << k1 + B << k2 + ... + B << kn
    res2 = lenn(B, A)
 
    # Compare the length of
    # the representations
    if (len(res1) > len(res2)):
        print(res2)
    else:
        print(res1)
 
# Driver Code
if __name__ == '__main__':
     
    # Product A X B
    A = 6
    B = 10
 
    # Function call
    minimumString(A, B)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the string
// which evaluates to the
// product of A and B
public static string len(long A, long B)
{
     
    // Stores the result
    string res = "";
    long log = 0;
 
    do
    {
         
        // 2 ^ log <= B &&
        // 2 ^ (log + 1) > B
        log = (long)(Math.Log(B) /
                     Math.Log(2));
 
        // Update res to
        // res+=A X 2^log
        if (log != 0)
        {
            res += A + "<<" + log;
        }
        else
        {
             
            // Update res to res+=A X 2^0
            res += A;
            break;
        }
 
        // Find the remainder
        B = B - (long)Math.Pow(2, log);
 
        // If remainder is not equal
        // to 0
        if (B != 0)
        {
            res += "+";
        }
        else
            break;
    } while (log != 0);
 
    // Return the resultant string
    return res;
}
 
// Function to find the minimum
// length representation of A*B
public static void minimumString(long A,
                                 long B)
{
     
    // Find representation of form
    // A << k1 + A << k2 + ... + A << kn
    string res1 = len(A, B);
 
    // Find representation of form
    // B << k1 + B << k2 + ... + B << kn
    string res2 = len(B, A);
 
    // Compare the length of
    // the representations
    if (res1.Length > res2.Length)
    {
        Console.WriteLine(res2);
    }
    else
    {
        Console.WriteLine(res1);
    }
}
 
// Driver Code
public static void Main()
{
     
    // Product A X B
    long A = 6;
    long B = 10;
 
    // Function call
    minimumString(A, B);
}
}
 
// This code is contributed by code_hunt


输出:
6<<3+6<<1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live