[Mảng 1 Chiều Cơ Bản]. Bài 35. Cộng trừ

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng A[] gồm N phần tử là các số nguyên, bạn hãy thực hiện phép tính cộng hoặc trừ N số nguyên này theo hướng dẫn. Bạn được cấp 1 mảng B[] gồm N - 1 phần tử đại diện cho N - 1 phép toán giữa N phần tử ban đầu trong mảng, trong đó 1 tương ứng với phép cộng và 2 tương ứng với phép trừ.

Ví dụ : A[] = {1, 2, 3, 4, 5} và B[] = {1, 1, 2, 1} ta sẽ tính giá trị của mảng A[] = 1 + 2 + 3 - 4 + 5


Đầu vào

Dòng 1 gồm N

Dòng 2 gồm N số của mảng A[]

Dòng 3 gồm N - 1 phần tử của mảng B[]


Giới hạn

1<=N<=1000

0<=A[i]<=100


Đầu ra

In ra kết quả của bài toán


Ví dụ :

Input 01
7
2 1 4 4 1 2 1 
2 2 2 1 2 1
Output 01
-7

Comments

Please read the guidelines before commenting.



  • 0
    tandatha140904  commented on Feb. 17, 2025, 5:16 p.m.

    include<bits/stdc++.h>

    using namespace std; int main(){ int n;cin>>n; int a[n],b[n-1]; for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=0;i<n-1;i++){ cin>>b[i]; } int cnt=0; int sum=a[0]; for(int i=1;i<n;i++){ if(b[cnt]==1){ sum += a[i]; } else{ sum -= a[i]; } cnt++; } cout<<sum; return 0; }


  • -2
    VDev  commented on Jan. 17, 2025, 4:40 p.m.

    FULL AC

    #include <bits/stdc++.h>
    #include <iomanip>
    #include <cmath>
    #include <climits>
    #define ll long long
    using namespace std;
    ll a[10000011], b[100011];
    ll cnt = 0, s = 0;
    
    int main(){
        ll n;
        cin >> n;
        for(ll i = 0; i < n; i++){
            cin >> a[i];
        }
        for(ll i = 0; i < n - 1; i++){
                cin >> b[i];
        }
        cnt = a[0];
        for(ll i = 0; i < n; i++){
            if(b[i] == 1){
                cnt += a[i + 1];
            }
            if(b[i] == 2){
            cnt -= a[i + 1];
            }
        }
        cout << cnt;
        return 0;
    }
    

  • -4
    Chu_manh_thang_cpp  commented on Nov. 30, 2024, 1:16 p.m.

    n = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) for i in range(n-1): if b[i] == 1: b[i] = '+' else: b[i] = '-' s ='' for i in range(n-1): s += str(a[i]) +str(b[i]) s += str(a[n-1]) ans = eval(s) print(ans)


    • 0
      TuanKhoi  commented on Dec. 1, 2024, 2:58 a.m.

      Sb