📜  将两个不同基数相乘并表示另一个给定基数的乘积

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

将两个不同基数相乘并表示另一个给定基数的乘积

给定两个数NM在基数XY和另一个基数P 。任务是找到NM的乘积并表示基数P的乘积。

例子:

方法:该方法是将给定的数字转换为十进制,执行乘积,然后将其转回以 p 为底的数字。请按照以下步骤操作:

  1. 将 N X和 M Y转换为十进制数。
  2. 对十进制数进行乘法运算。
  3. 将乘法结果从十进制转换为基数P

下面是上述方法的实现。

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Convert Number from a given base
// to decimal
// Return the value of a char.
static int value(char c)
{
  if (c >= '0' && c <= '9')
    return (int)c - '0';
  else
    return (int)c - 'A' + 10;
}
 
// Function to convert a
// number from given base to decimal
int toDecimal(string s, int base)
{
  int length = s.length();
 
  // Initialize power of base and result
  int power = 1, ans = 0;
 
  // Decimal equivalent of s
  for (int i = length - 1; i >= 0; i--)
  {
    ans += value(s[i]) * power;
    power = power * base;
  }
 
  return ans;
}
 
// Function to convert decimal
// to any given base
char reverseValue(int n)
{
  if (n >= 0 && n <= 9)
    return (char)(n + 48);
  else
    return (char)(n - 10 + 65);
}
 
// Function to convert a given
// decimal number to a base 'base'
string toBase(int base, int num)
{
  string s = "";
 
  // Convert input number is given
  // base by repeatedly dividing it
  // by base and taking remainder
  while (num > 0)
  {
    s += reverseValue(num % base);
    num /= base;
  }
  string sb = "";
 
  // Append a string into StringBuilder
  sb += (s);
 
  // Reverse the result
  reverse(sb.begin(), sb.end());
  return sb;
}
 
// Function to find
// the product of N and M
void findProduct(string N, int X,
                 string M,
                 int Y, int P)
{
 
  // Convert N to decimal
  int decimalX = toDecimal(N, X);
 
  // Convert y to decimal
  int decimalY = toDecimal(M, Y);
 
  // Multiply the decimal numbers
  int product = decimalX * decimalY;
 
  // Convert product to base
  string result = toBase(P, product);
 
  // Print the result
  cout << (result);
}
 
// Driver code
int main()
{
  string N = "101", M = "110";
  int X = 2, Y = 2, P = 16;
  findProduct(N, X, M, Y, P);
  return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java code to implement above approach
import java.io.*;
 
class GFG {
     
    // Function to find
    // the product of N and M
    static void findProduct(String N, int X,
                            String M,
                            int Y, int P)
    {
 
        // Convert N to decimal
        int decimalX = toDecimal(N, X);
 
        // Convert y to decimal
        int decimalY = toDecimal(M, Y);
 
        // Multiply the decimal numbers
        int product = decimalX * decimalY;
 
        // Convert product to base
        String result = toBase(P, product);
 
        // Print the result
        System.out.println(result);
    }
 
    // Convert Number from a given base
    // to decimal
    // Return the value of a char.
    static int value(char c)
    {
        if (c >= '0' && c <= '9')
            return (int)c - '0';
        else
            return (int)c - 'A' + 10;
    }
 
    // Function to convert a
    // number from given base to decimal
    static int toDecimal(String s, int base)
    {
        int length = s.length();
 
        // Initialize power of base and result
        int power = 1, ans = 0;
 
        // Decimal equivalent of s
        for (int i = length - 1; i >= 0; i--) {
            ans += value(s.charAt(i)) * power;
            power = power * base;
        }
 
        return ans;
    }
 
    // Function to convert decimal
    // to any given base
    static char reverseValue(int n)
    {
        if (n >= 0 && n <= 9)
            return (char)(n + 48);
        else
            return (char)(n - 10 + 65);
    }
 
    // Function to convert a given
    // decimal number to a base 'base'
    static String toBase(int base, int num)
    {
        String s = "";
 
        // Convert input number is given
        // base by repeatedly dividing it
        // by base and taking remainder
        while (num > 0) {
            s += reverseValue(num % base);
            num /= base;
        }
        StringBuilder sb = new StringBuilder();
 
        // Append a string into StringBuilder
        sb.append(s);
 
        // Reverse the result
        return new String(sb.reverse());
    }
     
    // Driver code
    public static void main(String[] args)
    {
        String N = "101", M = "110";
        int X = 2, Y = 2, P = 16;
        findProduct(N, X, M, Y, P);
    }
}


C#
// C# code to implement above approach
using System;
class GFG
{
 
  // Function to find
  // the product of N and M
  static void findProduct(String N, int X,
                          String M,
                          int Y, int P)
  {
 
    // Convert N to decimal
    int decimalX = toDecimal(N, X);
 
    // Convert y to decimal
    int decimalY = toDecimal(M, Y);
 
    // Multiply the decimal numbers
    int product = decimalX * decimalY;
 
    // Convert product to base
    String result = toBase(P, product);
 
    // Print the result
    Console.Write(result);
  }
 
  // Convert Number from a given base
  // to decimal
  // Return the value of a char.
  static int value(char c)
  {
    if (c >= '0' && c <= '9')
      return (int)c - '0';
    else
      return (int)c - 'A' + 10;
  }
 
  // Function to convert a
  // number from given base to decimal
  static int toDecimal(String s, int _base)
  {
    int length = s.Length;
 
    // Initialize power of base and result
    int power = 1, ans = 0;
 
    // Decimal equivalent of s
    for (int i = length - 1; i >= 0; i--)
    {
      ans += value(s[i]) * power;
      power = power * _base;
    }
 
    return ans;
  }
 
  // Function to convert decimal
  // to any given base
  static char reverseValue(int n)
  {
    if (n >= 0 && n <= 9)
      return (char)(n + 48);
    else
      return (char)(n - 10 + 65);
  }
 
  // Function to convert a given
  // decimal number to a base 'base'
  static String toBase(int _base, int num)
  {
    String s = "";
 
    // Convert input number is given
    // base by repeatedly dividing it
    // by base and taking remainder
    while (num > 0)
    {
      s += reverseValue(num % _base);
      num /= _base;
    }
    String sb = "";
 
    // Append a string into StringBuilder
    sb += s;
 
    // Reverse the result
    char[] arr = sb.ToCharArray();
    Array.Reverse(arr);
    return new string(arr);
  }
 
  // Driver code
  public static void Main()
  {
    String N = "101", M = "110";
    int X = 2, Y = 2, P = 16;
    findProduct(N, X, M, Y, P);
  }
}
 
// This code is contributed by saurabh_jaiswal.



输出
1E

时间复杂度: O(D) 其中 D 是 N、M 和乘积中的最大位数
辅助空间: O(1)