📜  减去给定基数的两个整数的程序

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

减去给定基数的两个整数的程序

给定三个正整数XYB ,其中XYBase-B整数,任务是找到X - Y的值,使得X >= Y

例子:

方法:给定的问题可以通过使用基本的数学减法来解决。请按照以下步骤解决给定的问题:

  • 初始化两个变量,说power = 1进位 = 0 ,以分别跟踪当前的功率和相减时产生的进位。
  • 初始化一个变量,比如finalVal = 0 ,以存储X – Y的结果值。
  • 迭代一个循环直到X > 0并执行以下步骤:
    • XY的当前值的最后一位存储在两个变量中,分别表示n1 = X % 10n2 = Y % 10
    • 通过更新X = X / 10Y = Y / 10XY中删除最后一位数字。
    • 初始化temp = n1 – n2 + 进位
    • 如果temp < 0 ,则将基数B添加到N ,即N = N + B并设置进位 = -1 ,这将充当借位。否则,设置进位 = 0
    • 将当前temp * power添加到 finalVal,即finalVal = finalVal + temp * power并设置power = power * 10
  • 完成上述步骤后,打印finalVal的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find X - Y in base B
int getDifference(int B, int X, int Y)
{
 
    // To store final answer
    int finalVal = 0;
 
    // To store carry generated
    int carry = 0;
 
    // To keep track of power
    int power = 1;
 
    while (X > 0) {
 
        // Store last digits of current
        // value of X and Y in n1 and
        // n2 respectively
        int n1 = X % 10;
        int n2 = Y % 10;
 
        // Remove last digits from
        // X and Y
        X = X / 10;
        Y = Y / 10;
 
        int temp = n1 - n2 + carry;
 
        if (temp < 0) {
 
            // Carry = -1 will act
            // as borrow
            carry = -1;
            temp += B;
        }
 
        else {
            carry = 0;
        }
 
        // Add in final result
        finalVal += temp * power;
        power = power * 10;
    }
 
    // Return final result
    return finalVal;
}
 
// Driver Code
int main()
{
    int X = 1212;
    int Y = 256;
    int B = 8;
 
    cout << (getDifference(B, X, Y));
 
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to find X - Y in base B
    public static int getDifference(
        int B, int X, int Y)
    {
 
        // To store final answer
        int finalVal = 0;
 
        // To store carry generated
        int carry = 0;
 
        // To keep track of power
        int power = 1;
 
        while (X > 0) {
 
            // Store last digits of current
            // value of X and Y in n1 and
            // n2 respectively
            int n1 = X % 10;
            int n2 = Y % 10;
 
            // Remove last digits from
            // X and Y
            X = X / 10;
            Y = Y / 10;
 
            int temp = n1 - n2 + carry;
 
            if (temp < 0) {
 
                // Carry = -1 will act
                // as borrow
                carry = -1;
                temp += B;
            }
 
            else {
                carry = 0;
            }
 
            // Add in final result
            finalVal += temp * power;
            power = power * 10;
        }
 
        // Return final result
        return finalVal;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int X = 1212;
        int Y = 256;
        int B = 8;
 
        System.out.println(
            getDifference(B, X, Y));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find X - Y in base B
def getDifference(B, X, Y) :
 
    # To store final answer
    finalVal = 0;
 
    # To store carry generated
    carry = 0;
 
    # To keep track of power
    power = 1;
 
    while (X > 0) :
 
        # Store last digits of current
        # value of X and Y in n1 and
        # n2 respectively
        n1 = X % 10;
        n2 = Y % 10;
 
        # Remove last digits from
        # X and Y
        X = X // 10;
        Y = Y // 10;
 
        temp = n1 - n2 + carry;
 
        if (temp < 0) :
 
            # Carry = -1 will act
            # as borrow
            carry = -1;
            temp += B;
 
        else :
            carry = 0;
 
        # Add in final result
        finalVal += temp * power;
        power = power * 10;
 
    # Return final result
    return finalVal;
 
# Driver Code
if __name__ == "__main__" :
 
    X = 1212;
    Y = 256;
    B = 8;
 
    print(getDifference(B, X, Y));
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
public class GFG
{
 
    // Function to find X - Y in base B
    public static int getDifference(int B, int X, int Y)
    {
 
        // To store final answer
        int finalVal = 0;
 
        // To store carry generated
        int carry = 0;
 
        // To keep track of power
        int power = 1;
 
        while (X > 0) {
 
            // Store last digits of current
            // value of X and Y in n1 and
            // n2 respectively
            int n1 = X % 10;
            int n2 = Y % 10;
 
            // Remove last digits from
            // X and Y
            X = X / 10;
            Y = Y / 10;
 
            int temp = n1 - n2 + carry;
 
            if (temp < 0) {
 
                // Carry = -1 will act
                // as borrow
                carry = -1;
                temp += B;
            }
 
            else {
                carry = 0;
            }
 
            // Add in final result
            finalVal += temp * power;
            power = power * 10;
        }
 
        // Return final result
        return finalVal;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int X = 1212;
        int Y = 256;
        int B = 8;
 
        Console.WriteLine(getDifference(B, X, Y));
    }
}
 
// This code is contributed by AnkThon


Javascript



输出:
734

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