Nhập một dãy A có N số tự nhiên (N<40) và 1 số K. Hãy xuất ra các phần tử có số lần xuất hiện trong dãy A từ K lần trở lên ( Mỗi số chỉ xuất 1 lần)
Dữ liệu nhập: file DAYSO.INP:
- Dòng 1: 2 số N, K giữa 2 số cách nhau 1 khoảng trắng
- Dòng 2: Dãy A
Kết quả: file DAYSO.OUT: xuất các số thỏa điều kiện trên, trường hợp không có số nào thỏa thì xuất số -1
DAYSO.INP DAYSO.OUT
6 2
1 2 2 3 6 6 2 6
Uses crt;
Trả lờiXóaVar n,k,i:longint;
f:array[1..40] of longint;
t:array[1..10000000] of longint;
procedure nhap;
Var fi:text;
Begin
assign(fi,'DAYSO.INP');
reset(fi);
read(fi,n,k);
For i:=1 to n do
read(fi,f[i]);
close(fi);
end;
procedure xuat;
Var fo:text;
Begin
assign(fo,'DAYSO.OUT');
rewrite(fo);
For i:=1 to n do
If t[f[i]]>=k then Begin t[f[i]]:=0; Write(fo,f[i]); Write(fo,' '); end;
close(fo);
end;
Begin
nhap;
fillchar(t,sizeof(t),0);
For i:=1 to n do
t[f[i]]:=t[f[i]]+1;
xuat;
end.
Nhận xét này đã bị tác giả xóa.
Trả lờiXóa#include
Trả lờiXóausing namespace std;
int main(int argc, char** argv) {
int n,k,l,a[100],dem;
cin >>n>>k>>l;
for (int i;i>a[i];
for(int i=1;i1)) break;
if (dem>=k)
{
cout <<a[i]<<" ";
break;
}
}
}
return 0;
}
#include <iostream>
Trả lờiXóa#include <fstream>
#include <algorithm>
using namespace std;
int main(int argc, char** argv) {
ofstream cout;
cout.open("D:/Dong/DAYSOout.txt");
ifstream cin;
cin.open("D:/Dong/DAYSOinp.txt");
int n,k,a[10000],dem=1,ds=0;
cin >>n>>k;
for (int i=0;i<n;i++) cin >>a[i];
sort(a,a+n);
a[n]=41;
for (int i=0;i<n;i++){
if (a[i]==a[i+1]) dem++;
else {
if (dem>=k){
cout <<a[i]<<" "<<dem<<endl;
ds=ds+1;
}
dem=1;
}
}
if (ds==0) cout <<"-1";
cin.close();
cout.close();
return 0;
}