[Mảng 1 Chiều Cơ Bản]. Bài 6. Đếm cặp số 1

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 số nguyên A[] gồm N phần tử, hãy đếm xem trong mảng A[] tồn tại bao nhiêu cặp số A[i] , A[j] với i khác j sao cho tổng của 2 phần tử này bằng số K cho trước.


Đầu vào

Dòng đầu tiên là số nguyên dương N

Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng

Dòng thứ 3 là số nguyên K


Giới hạn

1<=N<=1000

-10^3<=A[i]<=10^3


Đầu ra

In ra số lượng cặp thỏa mãn


Ví dụ :

Input 01
5
1 2 3 1 2
3
Output 01
4

Comments

Please read the guidelines before commenting.



  • -1
    naipret  commented on Sept. 15, 2025, 2:41 p.m. edited
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
    
      int num;
      cin >> num;
    
      vector<int> vec(num);
      for (int &ele : vec) {
        cin >> ele;
      }
    
      int target_sum;
      cin >> target_sum;
    
      int cnt = 0;
      for (int idx_i = 0; idx_i < num; idx_i++) {
        for (int idx_j = idx_i + 1; idx_j < num; idx_j++) {
          if (vec[idx_i] + vec[idx_j] == target_sum) {
            cnt++;
          }
        }
      }
    
      cout << cnt;
    }
    

  • 1
    SteepMoleCules  commented on Aug. 10, 2025, 3:36 a.m.

    include <bits/stdc++.h>

    using namespace std;

    using ll = long long;

    long long X[1000001];

    int main(){

    int n; cin >> n;
    
    int a[n];
    
    for(int i = 0; i < n; i++){
    
        cin >> a[i];
    
    }
    int k;
    
    cin>>k;
    
    ll res = 0;
    
    for(int x : a){
    
        int r = k-x;
    
        if(r>=0&&r<=1000000){
    
            res += X[r];
        }
        X[x]++;
    }
    cout << res << endl;
    

    }


  • 0
    id_07  commented on July 18, 2025, 9:20 a.m.
    CACH LAM BAI DEM CAP SO
    -TAO BIEN N,NHAP N,TAO BIEN DEM,DUYET MANG\
    -LAP 2 VONG I VA J BIEN J CAN LON HON I DE KO BI TRUNG
    
    #include <iostream>
    #include<cmath>
    #include <iomanip>
    using namespace std;
    
    int main() {
    int n;cin>>n;
    int a[n];
    for(int i = 0;i&lt;n;i++) cin>> a[i];
    
    int cap = 0;
    int k;cin>>k;
    for(int i = 0;i&lt;n;i++) {
        for(int j = i+1;j&lt;n;j++) {
            if(a[i] + a[j] == k) cap++;
        }
    }
    cout << cap;
    return 0;
    } 
    

  • -8
    NamKhanh187  commented on Feb. 14, 2025, 5:44 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -9
    phnam212  commented on Jan. 6, 2025, 9:43 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -17
    tansuperhot2010  commented on Sept. 23, 2024, 2:25 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -13
    tansuperhot2010  commented on Sept. 23, 2024, 2:23 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -14
    tansuperhot2010  commented on Sept. 23, 2024, 2:23 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -33
    Itachi  commented on April 30, 2024, 12:41 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.