Home  >  Article  >  Backend Development  >  Translate the following C++ code into Chinese: According to the given conditions, find the array that meets the conditions in the array

Translate the following C++ code into Chinese: According to the given conditions, find the array that meets the conditions in the array

王林
王林forward
2023-09-14 22:57:03999browse

Translate the following C++ code into Chinese: According to the given conditions, find the array that meets the conditions in the array

Suppose we have an array A containing n elements. There is another hidden array B of size n. These elements can be negative or positive. For each index i in the range 1 to n, the following will be done -

  • Initially set A[i] to 0

  • Then add B[i] to A[i], subtract B[i 1], then add B[i 2] and so on

We have to find the array B.

So if the input is something like A = [6, -4, 8, -2, 3] then the output will be [2, 4, 6, 1, 3]

Steps

To solve this problem we will follow the following steps-

for initialize i := 0, when i < size of A, update (increase i by 1),
do:
   print (A[i] + A[i + 1])

Example

Let us see the following implementation for better understanding-

#include <bits/stdc++.h>
using namespace std;
void solve(vector<int> A){
   for (int i = 0; i < A.size(); i++)
      cout << A[i] + A[i + 1] << ", ";
}
int main(){
   vector<int> A = { 6, -4, 8, -2, 3 };
   solve(A);
}

Input

{ 6, -4, 8, -2, 3 }

Output

2, 4, 6, 1, 3,

The above is the detailed content of Translate the following C++ code into Chinese: According to the given conditions, find the array that meets the conditions in the array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete