给定两个分别为M和N的正整数A和B的两个数组,任务是针对每i = 0到min(M,N)将A [i] + B [i]推入一个新数组,并打印新的最后生成数组。如果总和是两位数,则将这些位分解为两个元素,即结果数组的每个元素都必须是一个位数。
例子:
Input: A = {2, 3, 4, 5}, B = {1, 12, 3}
Output: 3 1 5 7 5
2 + 1 = 3
3 + 12 = 15 = 1 5
4 + 3 = 7
5
Hence the resultant array will be {3, 1, 5, 7, 5}
Input: A = {23, 5, 2, 7, 87}, B = {4, 67, 2, 8}
Output: 2 7 7 2 4 1 5 8 7
方法:创建一个向量来存储每次加法的结果。如果加法是单个数字,则将数字推入向量中,否则将数字分解为不同的数字,并将数组中的数字一一推入。最后打印向量的内容。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#include
using namespace std;
// Utility function to print the contents of the vector
void printVector(vector& result)
{
for (int i : result)
cout << i << " ";
}
// Recursive function to separate the digits of a positive
// integer and add them to the given vector
void split_number(int num, vector& result)
{
if (num > 0) {
split_number(num / 10, result);
result.push_back(num % 10);
}
}
// Function to add two arrays
void add(vector a, vector b)
{
// Vector to store the output
vector result;
int m = a.size(), n = b.size();
// Loop till a or b runs out
int i = 0;
while (i < m && i < n) {
// Get sum of next element from each array
int sum = a[i] + b[i];
// Separate the digits of sum and add them to
// the resultant vector
split_number(sum, result);
i++;
}
// Process remaining elements of first vector, if any
while (i < m) {
split_number(a[i++], result);
}
// Process remaining elements of second vector, if any
while (i < n) {
split_number(b[i++], result);
}
// Print the resultant vector
printVector(result);
}
// Driver code
int main()
{
// input vectors
vector a = { 23, 5, 2, 7, 87 };
vector b = { 4, 67, 2, 8 };
add(a, b);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
// Utility function to print
// the contents of the vector
static void printVector(Vector result)
{
for (int i : result)
{
System.out.print(i + " ");
}
}
// Recursive function to separate
// the digits of a positive integer
// and add them to the given vector
static void split_number(int num, Vector result)
{
if (num > 0)
{
split_number(num / 10, result);
result.add(num % 10);
}
}
// Function to add two arrays
static void add(Vector a, Vector b)
{
// Vector to store the output
Vector result = new Vector();
int m = a.size(), n = b.size();
// Loop till a or b runs out
int i = 0;
while (i < m && i < n)
{
// Get sum of next element from each array
int sum = a.get(i) + b.get(i);
// Separate the digits of sum and add them to
// the resultant vector
split_number(sum, result);
i++;
}
// Process remaining elements
// of first vector, if any
while (i < m)
{
split_number(a.get(i++), result);
}
// Process remaining elements
// of second vector, if any
while (i < n)
{
split_number(b.get(i++), result);
}
// Print the resultant vector
printVector(result);
}
// Driver code
public static void main(String[] args)
{
// input vectors
int[] arr1 = {23, 5, 2, 7, 87};
Vector a = new Vector<>();
for(Integer i:arr1)
a.add(i);
int[] arr2 = {4, 67, 2, 8};
Vector b = new Vector();
for(Integer i:arr2)
b.add(i);
add(a, b);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the
# above approach
# Utility function to print the
# contents of the list
def printVector(result):
for i in result:
print(i, end = " ")
# Recursive function to separate the
# digits of a positive integer and
# add them to the given list
def split_number(num, result):
if num > 0:
split_number(num // 10, result)
result.append(num % 10)
# Function to add two lists
def add(a, b):
# List to store the output
result = []
m, n = len(a), len(b)
# Loop till a or b runs out
i = 0
while i < m and i < n:
# Get sum of next element from
# each array
sum = a[i] + b[i]
# Separate the digits of sum and
# add them to the resultant list
split_number(sum, result)
i += 1
# Process remaining elements of
# first list, if any
while i < m:
split_number(a[i], result)
i += 1
# Process remaining elements of
# second list, if any
while i < n:
split_number(b[i], result)
i += 1
# Print the resultant list
printVector(result)
# Driver Code
if __name__ == "__main__":
# input lists
a = [23, 5, 2, 7, 87]
b = [4, 67, 2, 8]
add(a, b)
# This code is contributed by rituraj_jain
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Utility function to print
// the contents of the vector
static void printVector(List result)
{
foreach (int i in result)
{
Console.Write(i + " ");
}
}
// Recursive function to separate
// the digits of a positive integer
// and add them to the given vector
static void split_number(int num, List result)
{
if (num > 0)
{
split_number(num / 10, result);
result.Add(num % 10);
}
}
// Function to add two arrays
static void add(List a, List b)
{
// Vector to store the output
List result = new List();
int m = a.Count, n = b.Count;
// Loop till a or b runs out
int i = 0;
while (i < m && i < n)
{
// Get sum of next element from each array
int sum = a[i] + b[i];
// Separate the digits of sum and add them to
// the resultant vector
split_number(sum, result);
i++;
}
// Process remaining elements
// of first vector, if any
while (i < m)
{
split_number(a[i++], result);
}
// Process remaining elements
// of second vector, if any
while (i < n)
{
split_number(b[i++], result);
}
// Print the resultant vector
printVector(result);
}
// Driver code
public static void Main(String[] args)
{
// input vectors
int[] arr1 = {23, 5, 2, 7, 87};
List a = new List();
foreach(int i in arr1)
a.Add(i);
int[] arr2 = {4, 67, 2, 8};
List b = new List();
foreach(int i in arr2)
b.Add(i);
add(a, b);
}
}
// This code is contributed by princiraj1992
Javascript
输出:
2 7 7 2 4 1 5 8 7
时间复杂度: O(max(m,n))
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。