这是一道2012年东莞市程序设计竞赛镇区选拔赛的上机试题小学组,我现已上市级比赛,请大家帮忙。

2024-12-19 00:51:32
推荐回答(3个)
回答1:

var a,k:array[0..1000000]of longint;
b,c,d,e,f,g,h,i,j,l,n,m,mid:longint;
begin
assign(input,'count.in');
assign(output,'count.out');
reset(input);
rewrite(output);
read(b,f);
e:=0;
n:=0;
l:=1;
for c:=1 to b do
begin
read(d);
k[d]:=k[d]+1;
end;
while l<=b do
begin
for m:=1 to k[n] do
begin
a[l]:=n;
l:=l+1;
end;
n:=n+1;
end;
e:=0;
for c:=1 to b do
begin
i:=c+1;
j:=b;
while (j>=i) do
begin
mid:=(j+i) div 2;
if a[mid]+a[c]>f then j:=mid-1 else i:=mid+1;
end;
e:=e+(j-c);
end;
writeln(e);
close(input);
close(output);
end.

回答2:

c++方法:(有BUG)
#include
using namespace std;
int main()
{
int n,s,a[100001],k=0;
cin>>n>>s;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(a[i]+a[j]<=s)k++;
}
}
cout< return 0;
}

回答3:

var
begin
readln(n,s);
for i:=1 to n do read(a[i]);
for i:=1 to n do
begin
for j:=1 to n do
if a[i]+a[j] end;
writeln(k);
end.