统计数字
(count.pas/c/cpp)
【问题描述】
某次科研调查时得到了 个自然数,每个数均不超过1500000000(1.5*109)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。
【输入】
输入文件count.in包含 行:
第1行是整数 ,表示自然数的个数。
第2~ 行每行一个自然数。
【输出】
输出文件count.out包含 行( 为 个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。
【输入输出样例】
count.in | count.out |
8 2 4 2 4 5 100 2 100 | 2 3 4 2 5 1 100 2 |
【限制】
40%的数据满足:
80%的数据满足:
100%的数据满足: ,每个数均不超过1 500 000 000 (1.5*109)
======================
排序后扫描一遍..
========
var
n:longint;
a:array[1..200000]of longint;
procedure init;
begin
assign(input,'p1070.in');
assign(output,'p1070.out');
reset(input);
rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
procedure qsort(s,t:longint);
var
i,j:longint;
m,tem:longint;
begin
i:=s; j:=t; m:=a[(s+t)shr 1];
repeat
while m<a[j] do dec(j);
while m>a[i] do inc(i);
if i<=j then
begin
tem:=a[i];
a[i]:=a[j];
a[j]:=tem;
inc(i);
dec(j);
end;
until i>j;
if i<t then qsort(i,t);
if s<j then qsort(s,j);
end;
procedure main;
var
i:longint;
last,tot:longint;
begin
readln(n);
for i:=1 to n do
readln(a[i]);
qsort(1,n);
last:=a[1];
tot:=1;
for i:=2 to n do
begin
if last=a[i] then inc(tot);
if last<>a[i] then begin writeln(last,' ',tot); last:=a[i]; tot:=1; end;
end;
writeln(last,' ',tot);
end;
begin
init;
main;
terminate;
end.