Đế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.
Đế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.help meee
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.
viết phương trình đếm số lần xuất hiện của giá trị X trong mảng dãy số nguyên
help mee
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.
Lập trình C++. Đếm số lần xuất hiện các phần tử trong mảng sau đó in ra tần suất xuất hiện các số trong mảng. Dữ liệu vào: Cho file văn bản MAP1.INP gồm : dòng 1 chứa số nguyên N. Dòng 2: gồm N phần tử a1, a2,... Kết quả: ghi vào file MAP1.OUT gồm: mỗi dòng là phần tử và tần suất xuất hiện ( trên thứ tự xuất hiện từ trái sang phải)
#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!
viết chương trình nhập vào mảng a có n phần tử là các số nguyên dương.Tìm các số xuất hiện trong mảng a và số có số lần xuất hiện nhiều nhất
Đếm số
Cho một dãy gồm n số nguyên dương A1, A2, ..., An (N≤10,A, ≤ 10) và số 2.
Yêu cầu: Hãy đếm số lần xuất hiện của giá trị 2 trong mảng A.
Input
• Dòng đầu chứa số n và 2.
• Dòng thứ hai chứa n số nguyên dương A1, A2, …, An
Output
• Số lần xuất hiện số 2 trong mảng A.
```
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 main(){
int n, x;
cin >> n >> x;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
int count = 0;
for(int i = 0; i < n; i++){
if(a[i] == x){
count++;
}
}
cout << count << endl;
}
Cho một mảng a gồm 100 phần tử em hãy viết chương trình đếm số lần xuất hiện của biến x trên bàn phím. Please🤦
#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;
}
Viết chương trình nhập và xuất mảng có 100 phần tử các số nguyên
1/Nhập vào 1 số nguyên k, cho biết vị trí nào trong mảng bằng với k.
2/Nhập vào số nguyên h, cho biết h xuất hiện bao nhiêu lần trong mảng
3/ Tổng các phần tử của dãy
4/Số lượng các số hạng dương và tổng các số hạng dương
5/Số lượng các số hạng âm và tổng của các số hạng âmViết chương trình nhập và xuất mảng có 100 phần tử các số nguyên
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.
viết chương trình nhập vào 1 mảng 1 chiều gồm n phần tử ( với n được nhập vào từ bàn phím ) hãy thực hiện các công việc sau :
-nhập/xuất mảng
-đếm các số hoàn hảo có trong mảng
-tính tổng các phần tử chẵn trong mảng .
#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;
}
viết chương trình nhập vào 1 mảng 1 chiều gồm n phần tử ( với n được nhập vào từ bàn phím ) hãy thực hiện các công việc sau :
-nhập/xuất mảng
-đếm các số hoàn hảo có trong mảng
-tính tổng các phần tử chẵn trong mảng .
#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;
}