noip2007复赛提高组第一题答案

c语言

统计数字

谁有答案?

#include<stdio.h>
#include<string.h>
#define LEN 200000

int a[LEN],temp,mid;

int sort(int *a,int low,int high) //一趟快排
{

mid=a[low];
while (low<high)
{
while (low<high && a[high]>=mid) high--;
temp=a[low];a[low]=a[high];a[high]=temp;
while (low<high && a[high]<=mid) low++;
temp=a[low];a[low]=a[high];a[high]=temp;
}
return low;
}

void quicksort(int *a,int low,int high) //快排递归
{
//int mid;
if (low < high)
{
mid=sort(a,low,high);
quicksort(a,low,mid-1);
quicksort(a,mid+1,high);
}
}

int main()
{
FILE *fp;
int i,j,n,sum;

fp=fopen("count.in","r");
fscanf(fp,"%d",&n);
for (i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
fclose(fp);

quicksort(a,0,n-1); //调用快排

fp=fopen("count.out","w");
for (i=sum=0;i<n;i++) //统计不同数字的个数
if (a[i+1]!=a[i]) sum++;
for (n=1,i=0,j=0;i<sum;i++,j++) //边统计边输出
{
while (a[j+1]==a[j]) {n++; j++;}
fprintf(fp,"%d %d\n",a[j],n);
printf("%d %d\n",a[j],n);
n=1;
}
fclose(fp);

//return 1;
system("pause");
}
100%正确,后面有注释!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-05-25
var a:array[1..200000]of longint;
y:array[1..10000,1..2]of longint;
b,c,d,e,f,g,h,i,j,k,m,n:longint;
procedure paixu(i1,j1:longint);
var x,i2,j2:longint;
begin
x:=a[i1];i2:=i1;j2:=j1;
while i2<>j2 do
begin
while (i2<>j2)and(a[j2]>a[i2]) do dec(j2);
if i2<>j2 then begin a[i2]:=a[j2];a[j2]:=x;inc(i2);end;
while (i2<>j2)and(a[i2]<a[j2]) do inc(i2);
if i2<>j2 then begin a[j2]:=a[i2];a[i2]:=x;dec(j2);end;
end;
a[i2]:=x;
if i2-i1>=2 then paixu(i1,i2-1);
if j1-i2>=2 then paixu(i2+1,j1);
end;
begin
assign(input,'count.in');assign(output,'count.out');
reset(input);rewrite(output);
readln(n);
for i:=1 to n do readln(a[i]);
paixu(1,n);
g:=1;y[1,1]:=a[1];y[1,2]:=1;
for i:=2 to n do
begin
if a[i]=a[i-1] then begin inc(y[g,2]);continue;end;
writeln(y[g,1],' ',y[g,2]);
inc(g);
y[g,1]:=a[i];y[g,2]:=1;
end;
writeln(y[g,1],' ',y[g,2]);
close(input);close(output);
end.

这是pascal的
第2个回答  2019-01-20
#include<stdio.h>
#include<string.h>
#define
LEN
200000
int
a[LEN],temp,mid;
int
sort(int
*a,int
low,int
high)
//一趟快排
{
mid=a[low];
while
(low<high)
{
while
(low<high
&&
a[high]>=mid)
high--;
temp=a[low];a[low]=a[high];a[high]=temp;
while
(low<high
&&
a[high]<=mid)
low++;
temp=a[low];a[low]=a[high];a[high]=temp;
}
return
low;
}
void
quicksort(int
*a,int
low,int
high)
//快排递归
{
//int
mid;
if
(low
<
high)
{
mid=sort(a,low,high);
quicksort(a,low,mid-1);
quicksort(a,mid+1,high);
}
}
int
main()
{
FILE
*fp;
int
i,j,n,sum;
fp=fopen("count.in","r");
fscanf(fp,"%d",&n);
for
(i=0;i<n;i++)
fscanf(fp,"%d",&a[i]);
fclose(fp);
quicksort(a,0,n-1);
//调用快排
fp=fopen("count.out","w");
for
(i=sum=0;i<n;i++)
//统计不同数字的个数
if
(a[i+1]!=a[i])
sum++;
for
(n=1,i=0,j=0;i<sum;i++,j++)
//边统计边输出
{
while
(a[j+1]==a[j])
{n++;
j++;}
fprintf(fp,"%d
%d\n",a[j],n);
printf("%d
%d\n",a[j],n);
n=1;
}
fclose(fp);
//return
1;
system("pause");
}
100%正确,后面有注释!