Home > Backend Development > C++ > body text

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

王林
Release: 2023-09-14 22:57:03
forward
1052 people have browsed it

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])
Copy after login

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);
}
Copy after login

Input

{ 6, -4, 8, -2, 3 }
Copy after login

Output

2, 4, 6, 1, 3,
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!