Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
终于ac了In Reply To:大家注意了:不能用多输入 Posted by:xiaolijian at 2010-08-06 20:58:01 #include <stdio.h> #include <string.h> #define N 110 int n, k,f,c,e,d; __int64 a[N],temp,t[N];//其中a[N]为输入的元素数组 //输入并小到大排序 void iput() { int i,j,tem; for(i=0;i<n;i++) scanf("%I64d",&a[i]); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[j]<a[i]) { tem=a[i]; a[i]=a[j]; a[j]=tem; } } //n 个数为非整数持行函数 void feizheng() { int i; if(k%2==0) for(i=k-1;i>=0;i--) printf("%I64d ",a[i]); else for(i=n-1;i>=n-k;i--) printf("%I64d ",a[i]); } //正数持行函数 void zheng() { int i; for(i=n-1;i>=n-k;i--) printf("%I64d ",a[i]); } //以下两个表示既有正又有负的持行函数 void sh() { int i,j,times,tem; c=0; times=e; for(i=0,j=d-1;i<d,j>=0;i=i+2,j=j-2) {//每次取前两个元素乘积和最后两个元素的乘积进行比较,取乘积较大的两个元素。直到选出 temp 个元素 if(a[i]*a[i+1]>a[j]*a[j-1]) { c=c+2; //对t[]数组元素个数计数 t[times]=a[i]; t[times+1]=a[i+1]; } else { c=c+2; t[times]=a[j]; t[times]=a[j-1]; } times=times+2; if(c>=temp) break; } /*这里特别注意,哇了几次*/ if(k%2==0) c=c; else c=c+1; //大到小排序输出 for(i=0;i<c-1;i++) for(j=i+1;j<c;j++) if(t[i]<t[j]) { tem=t[i]; t[i]=t[j]; t[j]=tem; } // printf("the c is %d\n",c); for(i=0;i<c;i++) printf("%I64d ",t[i]); } void zhengfu() {//如果k 为偶数,则令temp=k;e=0;d=n; if(k%2==0) { temp=k; e=0; d=n; } //否则···· else { temp=k-1; t[0]=a[n-1]; d=n-1; e=1; } sh(); } int main() { scanf("%d%d",&n,&k); iput(); if(k==1) printf("%I64d\n",a[n-1]); else { if(a[n-1]<=0) feizheng(); else if(a[0]>=0) zheng(); else zhengfu(); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator