search
HomeBackend DevelopmentC++Given a constraint, add the elements of the given array
Given a constraint, add the elements of the given arraySep 05, 2023 pm 01:01 PM
array elementsRestrictionsAdd

Given a constraint, add the elements of the given array

For this problem, to add elements of two given arrays, we have some constraints, based on these constraints, the added values ​​will change. The sum of two given arrays a[] and b[] is stored into a third array c[] so that they give some elements in unit numbers. If the number of digits in the sum is greater than 1, the elements of the third array are split into two single-digit elements. For example, if the sum is 27, the third array will store it as 2,7.

Input: a[] = {1, 2, 3, 7, 9, 6}
       b[] = {34, 11, 4, 7, 8, 7, 6, 99}
Output: 3 5 1 3 7 1 4 1 7 1 3 6 9 9

Description

Output arrays and run a loop from the 0th index of both arrays. For each iteration of the loop, we consider the next elements in both arrays and add them. If the sum is greater than 9, we push the individual numbers of the sum to the output array, otherwise we push the sum itself to the output array. Finally, we push the remaining elements of the larger input array to the output array.

Example

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void split(int n, vector<int> &c) {
   vector<int> temp;
   while (n) {
      temp.push_back(n%10);
      n = n/10;
   }
   c.insert(c.end(), temp.rbegin(), temp.rend());
}
void addArrays(int a[], int b[], int m, int n) {
   vector<int> out;
   int i = 0;
   while (i < m && i < n) {
      int sum = a[i] + b[i];
      if (sum < 10) {
         out.push_back(sum);
      } else {
         split(sum, out);
      }
      i++;
   }
   while (i < m) {
      split(a[i++], out);
   }
   while (i < n) {
      split(b[i++], out);
   }
   for (int x : out)
   cout << x << " ";
}
int main() {
   int a[] = {1, 2, 3, 7, 9, 6};
   int b[] = {34, 11, 4, 7, 8, 7, 6, 99};
   int m =6;
   int n = 8;
   addArrays(a, b, m, n);
   return 0;
}

The above is the detailed content of Given a constraint, add the elements of the given array. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Java如何求数组元素的最大和最小值Java如何求数组元素的最大和最小值Oct 08, 2023 am 09:44 AM

Java中使用`Arrays.stream()`函数将数组转换为流,然后使用`min()`和`max()`函数来计算最小值和最大值。

数组元素的绝对差值之和最小的是哪一个?数组元素的绝对差值之和最小的是哪一个?Aug 29, 2023 am 10:09 AM

在这里,我们将看到一个有趣的问题。我们有一个包含N个元素的数组'a'。我们需要找到一个元素x,使得|a[0]-x|+|a[1]-x|+...+|a[n-1]-x|的值最小化。然后我们需要找到最小化的和。假设数组为:{1,3,9,6,3},现在x为3。所以和为|1-3|+|3-3|+|9-3|+|6-3|+|3-3|=11。为了解决这个问题,我们需要选择数组的中位数作为x。如果数组的大小是偶数,则会有两个中位数值。它们都是x的最佳选择。算法minSum(arr,n)begin&nbsp;&

PHP中如何使用implode函数将数组元素连接成字符串PHP中如何使用implode函数将数组元素连接成字符串Jun 26, 2023 pm 02:02 PM

在PHP编程中,implode函数是一种非常常用的函数,可以将一个数组中的元素连接成一个字符串。使用该函数可以节省开发人员编写大量连接字符串的代码,更加高效。implode的基本语法为:stringimplode(string$glue,array$pieces)该函数接收两个参数:$glue表示要连接数组元素的分隔符,$pieces表示

使用PHP的implode()函数将数组元素连接成带分隔符的字符串使用PHP的implode()函数将数组元素连接成带分隔符的字符串Nov 04, 2023 pm 02:37 PM

使用PHP的implode()函数将数组元素连接成带分隔符的字符串,代码示例如下:&lt;?php//定义一个数组$array=array('apple','banana','orange');//使用implode()函数将数组元素连接成带分隔符的字符串$delimiter=',';//定义分隔符$result=im

找到在给定约束条件下,通过N次操作从字符串'S'中删除N个字符后的值找到在给定约束条件下,通过N次操作从字符串'S'中删除N个字符后的值Aug 26, 2023 pm 10:29 PM

字符串的使用规范是什么?解决涉及给定字符串S的特定挑战。字符串S仅包含小写英文字母,并且在删除字符时必须遵循一定的约束。给定的约束是-字符串S中有小写英文字母只有在字符串中出现多次的字符才能删除。只能删除连续出现的字符。以下步骤可用于从字符串S中删除字符-在迭代字符串S时查找所有出现多次的字符。通过对每个字符再次迭代字符串S来查找所有连续出现的字符。如果字符连续出现的次数大于或等于迭代次数,则删除前N个出现的字符。继续执行步骤2和3,直到完成所有迭代。最后,通过返回最终的字符串S,可以发现经过N

在C语言中,出现多次的数组元素是什么?在C语言中,出现多次的数组元素是什么?Sep 05, 2023 am 09:05 AM

Array是一个容器,其中包含相同数据类型的元素,长度需要事先定义。数组中的元素可以以任何顺序和任意次数出现。因此,在这个程序中,我们将找出数组中出现多次的元素。问题描述-我们已经给出一个数组arr[],我们需要找出数组中重复出现的元素,并打印它们。让我们举一个例子来更好地理解。例子:Input:arr[]={5,11,11,2,1,4,2}Output:112解释我们有一个包含一些元素的数组arr,首先我们会在重复函数中比较下一个元素。重复函数用于在数组中找到重复的元素。在重复函数中,我们使用

使用PHP array_keys()函数获取数组中的元素键使用PHP array_keys()函数获取数组中的元素键Jun 27, 2023 am 08:54 AM

在使用PHP开发过程中,经常需要操作数组。而在数组中,我们通常会需要获取元素的键值,以便于后续操作。为此,PHP提供了一个非常方便的函数array_keys(),能够快速地从数组中获取元素的键。array_keys()函数的用法非常简单,其基本语法如下:arrayarray_keys(array$array[,mixed$search_valu

Python程序递归线性搜索数组中的元素Python程序递归线性搜索数组中的元素Aug 20, 2023 pm 11:22 PM

线性搜索是在数组中搜索元素的最简单方法。它是一种顺序搜索算法,从一端开始,并检查数组的每个元素,直到找到所需的元素。递归是指一个函数调用自身,当使用递归函数时,我们需要使用任何循环来生成迭代。下面的语法演示了简单递归函数的工作原理。defrerecursiveFun():Statements...rerecursiveFun()...rerecursiveFun递归地线性搜索元素从数组中递归地线性搜索一个元素,只能通过使用函数来实现。在Python中,要定义一个函数,我们需要使用def关键字。在

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools