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 |
Re:在计算total=total+t*(array[j]-array[i]);时产生了溢出,哈哈In Reply To:两种方法,一种过了,还有一种没有过,奇怪了~~ Posted by:lihuiyykl at 2009-07-22 22:14:11 > 用了两种方法,一种过了,还有一种没有过,反复测试了N多种数据,并没有发现第二种方法的问题,有时间大家帮忙找找茬:贴码下 > #include<iostream> > #include<cmath> > > using namespace std; > #define MAX 100000000 > int array[MAX]; > > int comp(const void *a,const void *b) > { > return *(int *)a-*(int *)b; > } > > int main() > { > long long n; > while(cin>>n) > { > for(int i=0;i<n;i++) cin>>array[i]; > qsort(array,n,sizeof(int),comp); > long long total=0; > int t=n-1; > int i,j; > i=0;j=n-1; > while(t>0) > { > total=total+t*(array[j]-array[i]); > t=t-2; > i++; > j--; > } > total=2*total; > cout<<total<<endl; > } > return 0; > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator