Submit solution
Points:
1.00 (partial)
Time limit:
1.0s
Memory limit:
256M
Input:
stdin
Output:
stdout
Author:
Problem source:
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
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; }
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); }
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.
This comment is hidden due to too much negative feedback. Show it anyway.