代码1:
//pat1045v1
#include <stdio.h>
#include <stdlib.h>
int cmp(const void* a,const void* b){
return *(int*)a-*(int*)b;
}
int main(){
int n,i,j,max=0,k=0; scanf("%d",&n);
long int a[n],b[n];
for(i=0;i<n;i++){
scanf("%ld",&a[i]);
b[i]=a[i];
}
qsort(b,n,sizeof(long int),cmp);
int res[n];
for(i=0;i<n;i++){
if(a[i]>max) max=a[i];
if(max==a[i]&&a[i]==b[i]) res[k++]=a[i];
}
printf("%d\n",k);
for(i=0;i<k;i++)
printf("%d%c",res[i],i==k-1?'\n':' ');
return 0;
}
代码2:
//pat1045v1
#include <stdio.h>
#include <stdlib.h>
int cmp(const void* a,const void* b){
return *(int*)a-*(int*)b;
}
int main(){
int n,i,j,max=0,k=0; scanf("%d",&n);
long int a[n],b[n];
for(i=0;i<n;i++){
scanf("%ld",&a[i]);
b[i]=a[i];
}
qsort(b,n,sizeof(long int),cmp);
int res[n];
for(i=0;i<n;i++){
if(a[i]>max) max=a[i];
if(max==a[i]&&a[i]==b[i]) res[k++]=a[i];
}
printf("%d\n",k);
for(i=0;i<k;i++)
if(i==0)
printf("%d",res[i]);
else
printf(" %d",res[i]);
printf("\n");
return 0;
}