Đếm số lần xuất hiện của giá trị x trong mảng A.Đếm số lần xuất hiện của các phần tử trong mảng.
Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.


program dem_so_lan_xuat_hien;
uses crt;
var
A: array[1..100] of integer;
N, X, i, count: integer;
begin
clrscr;
write('Nhap so phan tu mang A: '); readln(N);
for i := 1 to N do
begin
write('A[',i,'] = ');
readln(A[i]);
end;
write('Nhap gia tri can tim x: '); readln(X);
count := 0;
for i := 1 to N do
begin
if A[i] = X then
count := count + 1;
end;
writeln('So lan xuat hien cua ', X, ' trong mang A la: ', count);
readln;
end.

program tinh_so_lan_xuat_hien;
uses crt;
var i,n,x,d:integer;
a:array[1..100]of integer;
begin
clrscr;
write('nhap n:');readln(n);
for i:=1 to n do
begin
write('a[',i,']=');readln(a[i]);
end;
d:=0;
write('nhap x:');readln(x);
for i:=1 to n do
if x=a[i] then inc(d);
writeln(x,' xuat hien ',d,' lan trong mang a');
readln;
end.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll> mp;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
freopen("MAP1.INP","r",stdin);
freopen("MAP1.OUT","w",stdout);
ll n; cin >> n;
ll a[n+5];
for(ll i=1;i<=n;i++) cin >> a[i], mp[a[i]]++;
for(pair<ll,ll> it:mp) cout << it.first << " " << it.second << "\n";
}
Chúc bạn học tốt!

```
n, k = map(int, input().split())
a = list(map(int, input().split()))
count = 0
for i in range(n):
if a[i] == k:
count += 1
print(count)
```
giải thích: dòng đầu đọc vào số n và giá trị k, dòng hai đọc vào mảng a. Biến count được khởi tạo bằng 0 để đếm số lần xuất hiện của giá trị k trong mảng a. Vòng lặp for duyệt qua từng phần tử trong mảng a. Nếu phần tử đó bằng k => tăng biến count lên 1. Sau cùng, in ra giá trị của biến count.
Ví dụ:
Input:
```
5 2
1 2 3 2 4
```
Output:
```
2
```
(Giá trị 2 xuất hiện 2 lần trong mảng [1, 2, 3, 2, 4].)

#include <bits/stdc++.h>
using namespace std;
int n,x,i,cnt,res=0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>x;
for(i=0;i<n;i++)
{
cin>>cnt;
if(cnt==x) res++;
}
cout<<"so luong xuat hien so "<<x<<" la: "<<res;
return 0;
}

uses crt;
var a:array[1..100]of integer;
i,n,k,h,dem,t,dem1,dem2,t1,t2:integer;
begin
clrscr;
for i:=1 to 100 do
begin
write('A[',i,']='); readln(a[i]);
end;
n:=100;
write('Nhap k='); readln(k);
for i:=1 to n do
if a[i]=k then write(i:4);
writeln;
write('Nhap h='); readln(h);
dem:=0;
for i:=1 to n do
if a[i]=h then inc(dem);
writeln(h,' xuat hien ',dem,' lan trong mang');
t:=0;
for i:=1 to n do
t:=t+a[i];
writeln('Tong cac phan tu cua day la: ',t);
dem1:=0;
t1:=0;
dem2:=0;
t2:=0;
for i:=1 to n do
begin
if a[i]>0 then
begin
inc(dem1);
t1:=t1+a[i];
end;
if a[i]<0 then
begin
inc(dem2);
t2:=t2+a[i];
end;
end;
writeln('Co ',dem1,' so duong');
writeln('Tong cac so duong la: ',t1);
writeln('Co ',dem2,' so am');
writeln('Tong cac so am la: ',t2);
readln;
end.

#include <bits/stdc++.h>
using namespace std;
long long a[1000],n,i,dem,t,j,t1;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
dem=0;
for (i=1; i<=n; i++)
{
t=0;
for (j=1; j<=a[i]-1; j++)
if (a[i]%j==0) t+=j;
if (t==a[i]) dem++;
}
cout<<dem<<endl;
t1=0;
for (i=1; i<=n; i++)
if (a[i]%2==0) t1+=a[i];
cout<<t1;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
long long a[1000],n,i,dem,t,j,t1;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
dem=0;
for (i=1; i<=n; i++)
{
t=0;
for (j=1; j<=a[i]-1; j++)
if (a[i]%j==0) t+=j;
if (t==a[i]) dem++;
}
cout<<dem<<endl;
t1=0;
for (i=1; i<=n; i++)
if (a[i]%2==0) t1+=a[i];
cout<<t1;
return 0;
}

#include <bits/stdc++.h>
using namespace std;
long long a[1000],n,i,dem,t,j,t1;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
dem=0;
for (i=1; i<=n; i++)
{
t=0;
for (j=1; j<=a[i]-1; j++)
if (a[i]%j==0) t+=j;
if (t==a[i]) dem++;
}
cout<<dem<<endl;
t1=0;
for (i=1; i<=n; i++)
if (a[i]%2==0) t1+=a[i];
cout<<t1;
return 0;
}