📜  程序将给定基数的两个整数相加

📅  最后修改于: 2021-04-24 21:56:48             🧑  作者: Mango

给定三个整数XYB ,其中X和Y是Base-B整数。任务是找到整数XY的总和。

例子:

Input: X = 123, Y = 234, B = 6
Output: 401
Explanation:
Sum of two integers in base 6 - 
     1 1   
     1 2 3
+    2 3 4
-------------
     4 0 1

Input: X = 546, Y = 248 B = 9
Output: 805
Explanation:
Sum of two integers in base 9 - 
     1 1
     5 4 6
+    2 4 8
-------------
     8 0 5   

方法:想法是利用这样的事实:每当数字的两位数字相加时,则位值将是数字总和乘以底数的模数,而进位将是数字总和除以底数的整数除法。 IE

Let two digits of the number be D1 and D2 -
Place Value = (D1 + D2) % B

Carry = (D1 + D2) / B

同样,将最后一位加上每个数字以得到所需的结果。

下面是上述方法的实现:

C++
// C++ implementation to find the
// sum of two integers of base B
  
#include 
  
using namespace std;
  
// Function to find the sum of 
// two integers of base B
string sumBaseB(string a, 
      string b, int base)
{
    int len_a, len_b;
  
    len_a = a.size();
    len_b = b.size();
  
    string sum, s;
    s = "";
    sum = "";
  
    int diff;
    diff = abs(len_a - len_b);
      
    // Padding 0 in front of the 
    // number to make both numbers equal
    for (int i = 1; i <= diff; i++)
        s += "0";
      
    // Condition to check if the strings
    // have lengths mis-match
    if (len_a < len_b)
        a = s + a;
    else
        b = s + b;
  
    int curr, carry = 0;
      
    // Loop to find the find the sum 
    // of two integers of base B
    for (int i = max(len_a, len_b) - 1; 
                           i > -1; i--) {
          
        // Current Place value for 
        // the resultant sum
        curr = carry + (a[i] - '0') + 
                       (b[i] - '0');
  
        // Update carry
        carry = curr / base;
  
        // Find current digit
        curr = curr % base;
  
        // Update sum result
        sum = (char)(curr + '0') + sum;
    }
    if (carry > 0)
        sum = (char)(carry + '0') + sum;
    return sum;
}
  
// Driver Code
int main()
{
    string a, b, sum;
    int base;
    a = "123";
    b = "234";
    base = 6;
      
    // Function Call
    sum = sumBaseB(a, b, base);
    cout << sum << endl;
    return 0;
}


Java
// Java implementation to find the 
// sum of two integers of base B 
class GFG {
  
    // Function to find the sum of 
    // two integers of base B 
    static String sumBaseB(String a, String b, int base) 
    { 
        int len_a, len_b; 
      
        len_a = a.length(); 
        len_b = b.length(); 
      
        String sum, s; 
        s = ""; 
        sum = ""; 
      
        int diff; 
        diff = Math.abs(len_a - len_b); 
          
        // Padding 0 in front of the 
        // number to make both numbers equal 
        for (int i = 1; i <= diff; i++) 
            s += "0"; 
          
        // Condition to check if the strings 
        // have lengths mis-match 
        if (len_a < len_b) 
            a = s + a; 
        else
            b = s + b; 
      
        int curr, carry = 0; 
          
        // Loop to find the find the sum 
        // of two integers of base B 
        for (int i = Math.max(len_a, len_b) - 1; 
                            i > -1; i--) { 
              
            // Current Place value for 
            // the resultant sum 
            curr = carry + (a.charAt(i) - '0') + 
                        (b.charAt(i) - '0'); 
      
            // Update carry 
            carry = curr / base; 
      
            // Find current digit 
            curr = curr % base; 
      
            // Update sum result 
            sum = (char)(curr + '0') + sum; 
        } 
        if (carry > 0) 
            sum = (char)(carry + '0') + sum; 
        return sum; 
    } 
      
    // Driver Code 
    public static void main (String[] args) 
    { 
        String a, b, sum; 
        int base; 
        a = "123"; 
        b = "234"; 
        base = 6; 
          
        // Function Call 
        sum = sumBaseB(a, b, base); 
        System.out.println(sum); 
    } 
}
  
// This code is contributed by AnkitRai01


Python 3
# Python 3 implementation to find the
# sum of two integers of base B
   
# Function to find the sum of 
# two integers of base B
def sumBaseB(a,b,base):
   
    len_a = len(a)
    len_b = len(b)
  
    s = "";
    sum = "";
   
    diff = abs(len_a - len_b);
       
    # Padding 0 in front of the 
    # number to make both numbers equal
    for i in range(1,diff+1):
        s += "0"
       
    # Condition to check if the strings
    # have lengths mis-match
    if (len_a < len_b):
        a = s + a
    else:
        b = s + b;
   
    carry = 0;
       
    # Loop to find the find the sum 
    # of two integers of base B
    for i in range(max(len_a, len_b) - 1,-1,-1):
           
        # Current Place value for 
        # the resultant sum
        curr = carry + (ord(a[i]) -ord('0')) +( ord(b[i]) - ord('0'));
   
        # Update carry
        carry = curr // base
   
        # Find current digit
        curr = curr % base;
   
        # Update sum result
        sum = chr(curr + ord('0')) + sum
          
    if (carry > 0):
        sum = chr(carry + ord('0')) + sum;
    return sum
   
# Driver Code
  
a = "123"
b = "234"
base = 6
       
# Function Call
sum = sumBaseB(a, b, base);
print(sum)
  
# This code is contributed by atul_kumar_shrivastava


C#
// C# implementation to find the 
// sum of two integers of base B 
using System;
  
class GFG {
  
    // Function to find the sum of 
    // two integers of base B 
    static string sumBaseB(string a, string b, int base_var) 
    { 
        int len_a, len_b; 
      
        len_a = a.Length; 
        len_b = b.Length; 
      
        string sum, s; 
        s = ""; 
        sum = ""; 
      
        int diff; 
        diff = Math.Abs(len_a - len_b); 
          
        // Padding 0 in front of the 
        // number to make both numbers equal 
        for (int i = 1; i <= diff; i++) 
            s += "0"; 
          
        // Condition to check if the strings 
        // have lengths mis-match 
        if (len_a < len_b) 
            a = s + a; 
        else
            b = s + b; 
      
        int curr, carry = 0; 
          
        // Loop to find the find the sum 
        // of two integers of base B 
        for (int i = Math.Max(len_a, len_b) - 1; 
                            i > -1; i--) { 
              
            // Current Place value for 
            // the resultant sum 
            curr = carry + (a[i] - '0') + 
                        (b[i] - '0'); 
      
            // Update carry 
            carry = curr / base_var; 
      
            // Find current digit 
            curr = curr % base_var; 
      
            // Update sum result 
            sum = (char)(curr + '0') + sum; 
        } 
        if (carry > 0) 
            sum = (char)(carry + '0') + sum; 
        return sum; 
    } 
      
    // Driver Code 
    public static void Main (string[] args) 
    { 
        string a, b, sum; 
        int base_var; 
        a = "123"; 
        b = "234"; 
        base_var = 6; 
          
        // Function Call 
        sum = sumBaseB(a, b, base_var); 
        Console.WriteLine(sum); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
401