[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
    NamKhanh187  commented on Feb. 14, 2025, 5:44 a.m.

    include <bits/stdc++.h>

    using namespace std; int arr[1001]; int main() { iosbase::syncwithstdio(false); cin.tie(NULL); unorderedmap<int, int> mp; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; mp[arr[i]]++; } int x; cin >> x; int count = 0; for (int i = 0; i < n; i++) { count += mp[x - arr[i]]; if (arr[i] * 2 == x) count--; } cout << count / 2; return 0; }


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

    include<bits/stdc++.h>

    using namespace std;

    int check(int a[], int n, int k){ int cnt=0; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++){ if(a[i]+a[j]==k){ cnt+=1; } } } return cnt; }

    int main(){ int n;cin>>n; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } int k;cin>>k; check(a,n,k); cout<<check(a,n,k); }


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

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


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

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


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

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


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

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