| ||||||||||
| 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 | |||||||||
按照大家的讨论写了一个程序,可总是WA,为什么会回这样呢?还有就是一个集合的元素可以相同啊?#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int ab[16000001];
int cd[16000001];
int a[4001],b[4001],c[4001],d[4001];
int main()
{
int n,i,j,num;
int number;
int binary_search(int,int);
while(scanf("%d",&n)==1)
{
for(i=0;i<n;i++) scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
num=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
ab[num++]=a[i]+b[j];//枚举a,b
num=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cd[num++]=c[i]+d[j];//枚举c,d
sort(cd,cd+n*n);//对c+d所得的数进行排序
number=0;
for(i=0;i<n*n;i++)
if(binary_search(n,-ab[i])==1) number++;//在cd中二分查找ab中元素的相反数
printf("%d\n",number);
}
return 0;
}
int binary_search(int n,int key)
{
int low=0,high=n*n-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(cd[mid]==key) return 1;
if(cd[mid]>key) high=mid-1;
else low=mid+1;
}
return 0;
}
有没有这样的输入呢?
6
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
有的话答案是多少呢?
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator