给定2个相同长度的数字a和b 。任务是这样计算它们的总和:在相加两个对应位置时,进位仅与它们保持在一起,而不是向左传播。
请参见下图以供参考:
例子:
Input: a = 7752 , b = 8834
Output: 151586
Input: a = 123 , b = 456
Output: 579
方法:首先,将数字a和b都反转。现在,要生成结果总和:
- 从a和b中提取数字。
- 计算数字总和。
- 如果数字总和是一个数字,则将其直接附加到结果总和上。
- 否则,将当前计算的数字总和取反,并从中一一提取数字,然后附加到结果总和上。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to print sum of 2 numbers
// without propagating carry
int printSum(int a, int b)
{
int res = 0;
int temp1 = 0, temp2 = 0;
// Reverse a
while(a)
{
temp1 = temp1*10 + (a%10);
a /= 10;
}
a = temp1;
// Reverse b
while(b)
{
temp2 = temp2*10 + (b%10);
b /= 10;
}
b = temp2;
// Generate sum
// Since length of both a and b are same,
// take any one of them.
while(a)
{
// Extract digits from a and b and add
int sum = (a%10 + b%10);
// If sum is single digit
if(sum/10 == 0)
res = res*10 + sum;
else
{
// If sum is not single digit
// reverse sum
temp1 = 0;
while(sum)
{
temp1 = temp1*10 + (sum%10);
sum /= 10;
}
sum = temp1;
// Extract digits from sum and append
// to result
while(sum)
{
res = res*10 + (sum%10);
sum /=10;
}
}
a/=10;
b/=10;
}
return res;
}
// Driver code
int main()
{
int a = 7752, b = 8834;
cout<
Java
// Java implementation of the approach
class GFG
{
// Function to print sum of 2 numbers
// without propagating carry
static int printSum(int a, int b)
{
int res = 0;
int temp1 = 0, temp2 = 0;
// Reverse a
while (a != 0)
{
temp1 = temp1 * 10 + (a % 10);
a /= 10;
}
a = temp1;
// Reverse b
while (b != 0)
{
temp2 = temp2 * 10 + (b % 10);
b /= 10;
}
b = temp2;
// Generate sum
// Since length of both a and b are same,
// take any one of them.
while (a != 0)
{
// Extract digits from a and b and add
int sum = (a % 10 + b % 10);
// If sum is single digit
if (sum / 10 == 0)
{
res = res * 10 + sum;
}
else
{
// If sum is not single digit
// reverse sum
temp1 = 0;
while (sum != 0)
{
temp1 = temp1 * 10 + (sum % 10);
sum /= 10;
}
sum = temp1;
// Extract digits from sum and append
// to result
while (sum != 0)
{
res = res * 10 + (sum % 10);
sum /= 10;
}
}
a /= 10;
b /= 10;
}
return res;
}
// Driver code
public static void main(String[] args)
{
int a = 7752, b = 8834;
System.out.println(printSum(a, b));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function to prsum of 2 numbers
# without propagating carry
def printSum(a, b):
res, temp1, temp2 = 0, 0, 0
# Reverse a
while a > 0:
temp1 = temp1 * 10 + (a % 10)
a //= 10
a = temp1
# Reverse b
while b > 0:
temp2 = temp2 * 10 + (b % 10)
b //= 10
b = temp2
# Generate sum
# Since length of both a and b are same,
# take any one of them.
while a:
# Extract digits from a and b and add
Sum = a % 10 + b % 10
# If sum is single digit
if Sum // 10 == 0:
res = res * 10 + Sum
else:
# If sum is not single digit
# reverse sum
temp1 = 0
while Sum > 0:
temp1 = temp1 * 10 + (Sum % 10)
Sum //= 10
Sum = temp1
# Extract digits from sum and
# append to result
while Sum > 0:
res = res * 10 + (Sum % 10)
Sum //= 10
a //= 10
b //= 10
return res
# Driver code
if __name__ == "__main__":
a, b = 7752, 8834
print(printSum(a, b))
# This code is contributed
# by Rituraj Jain
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to print sum of 2 numbers
// without propagating carry
static int printSum(int a, int b)
{
int res = 0;
int temp1 = 0, temp2 = 0;
// Reverse a
while(a != 0)
{
temp1 = temp1 * 10 + (a % 10);
a /= 10;
}
a = temp1;
// Reverse b
while(b != 0)
{
temp2 = temp2 * 10 + (b % 10);
b /= 10;
}
b = temp2;
// Generate sum
// Since length of both a and b are same,
// take any one of them.
while(a != 0)
{
// Extract digits from a and b and add
int sum = (a % 10 + b % 10);
// If sum is single digit
if(sum / 10 == 0)
res = res * 10 + sum;
else
{
// If sum is not single digit
// reverse sum
temp1 = 0;
while(sum != 0)
{
temp1 = temp1 * 10 + (sum % 10);
sum /= 10;
}
sum = temp1;
// Extract digits from sum and append
// to result
while(sum != 0)
{
res = res * 10 + (sum % 10);
sum /=10;
}
}
a /= 10;
b /= 10;
}
return res;
}
// Driver code
public static void Main()
{
int a = 7752, b = 8834;
Console.Write(printSum(a, b));
}
}
// This code is contributed
// by Akanksha Rai
PHP
Javascript
输出:
151586
时间复杂度: O(N)。