给出两个正数n和m。您只需要简单地找到两个数字的加法,但在给定条件下,该加法中没有任何进位系统。在更高的MSB上不添加进位。
例子 :
Input : m = 456, n = 854
Output : 200
Input : m = 456, n = 4
Output : 450
算法 :
Input n, m while(n||m)
{
// Add each bits
bit_sum = (n%10) + (m%10);
// Neglect carry
bit_sum %= 10;
// Update result
// multiplier to maintain place value
res = (bit_sum * multiplier) + res;
n /= 10;
m /= 10;
// Update multiplier
multiplier *=10;
} print res
方法 :
为了解决这个问题,我们将需要对数字进行逐位加法,即从最右边的位(LSB)开始加两个数,并从两个相同位置的nuber中加整数。同样,我们将忽略每个位置的进位,以使进位不会影响更高的位。
开始将两个数字逐位相加,然后对每个位取整数和,然后通过对bit_sum取模乘以10来忽略它们的进位,通过将bit_sum与指定位值的乘数相乘,再将bit_sum加到res上。 (乘数在每次迭代中增加10倍。)
下面是上述方法的实现:
C++
// CPP program for special
// addition of two number
#include
using namespace std;
int xSum(int n, int m)
{
// variable to store result
int res = 0;
// variable to maintain
// place value
int multiplier = 1;
// variable to maintain
// each digit sum
int bit_sum;
// Add numbers till each
// number become zero
while (n || m) {
// Add each bits
bit_sum = (n % 10) + (m % 10);
// Neglect carry
bit_sum %= 10;
// Update result
res = (bit_sum * multiplier) + res;
n /= 10;
m /= 10;
// Update multiplier
multiplier *= 10;
}
return res;
}
// Driver program
int main()
{
int n = 8458;
int m = 8732;
cout << xSum(n, m);
return 0;
}
Java
// Java program for special
// addition of two number
import java.util.*;
import java.lang.*;
public class GfG {
public static int xSum(int n, int m)
{
int res = 0;
int multiplier = 1;
int bit_sum;
// Add numbers till each
// number become zero
while (true) {
if(n==0 && m==0)
break;
// Add each bits
bit_sum = (n % 10) + (m % 10);
// Neglect carry
bit_sum %= 10;
// Update result
res = (bit_sum * multiplier) + res;
n /= 10;
m /= 10;
// Update multiplier
multiplier *= 10;
}
return res;
}
// Driver function
public static void main(String args[])
{
int n = 8458;
int m = 8732;
System.out.println(xSum(n, m));
}
}
/* This code is contributed by Sagar Shukla */
Python3
# Python3 program for special
# addition of two number
import math
def xSum(n, m) :
# variable to
# store result
res = 0
# variable to maintain
# place value
multiplier = 1
# variable to maintain
# each digit sum
bit_sum = 0
# Add numbers till each
# number become zero
while (n or m) :
# Add each bits
bit_sum = ((n % 10) +
(m % 10))
# Neglect carry
bit_sum = bit_sum % 10
# Update result
res = (bit_sum *
multiplier) + res
n = math.floor(n / 10)
m = math.floor(m / 10)
# Update multiplier
multiplier = multiplier * 10
return res
# Driver code
n = 8458
m = 8732
print (xSum(n, m))
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# program for special
// addition of two number
using System;
public class GfG {
public static int xSum(int n, int m)
{
int res = 0;
int multiplier = 1;
int bit_sum;
// Add numbers till each
// number become zero
while (true) {
// Add each bits
bit_sum = (n % 10) + (m % 10);
// Neglect carry
bit_sum %= 10;
// Update result
res = (bit_sum * multiplier) + res;
n /= 10;
m /= 10;
// Update multiplier
multiplier *= 10;
if (n == 0)
break;
if (m == 0)
break;
}
return res;
}
// Driver function
public static void Main()
{
int n = 8458;
int m = 8732;
Console.WriteLine(xSum(n, m));
}
}
/* This code is contributed by Vt_m */
PHP
Javascript
输出 :
6180