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 |
和我比慢 1000MS 过。哈哈哈哈。证明数据只到900000 没有1000000 呵呵。第一次使用C++写ACM 感觉好爽。但是 代码就写的很对不起观众了 #include<iostream> #include<stdlib.h> #include<math.h> #include<stdio.h> using namespace std; class Pint{ public : int *init,j; int judge(int n){ int i,j,flag=1; j=sqrt((double)n); for(i=2;i<=j;i++) if(n%i==0){ flag=0; break; } return flag; } int Binary(int init[],int n,int key){ int i,right,mid,left; right=n-1;left=0; while(left<=right){ mid=(right+left)/2; if(init[mid]==key){ return mid; }else{ if(init[mid]<key) left=mid+1; else right=mid-1; } } return -1; } Pint(){ int i,n; init=(int *)malloc(900000*sizeof(int)); for(i=3,n=0;i<900000;i++) if(judge(i)) init[n++]=i; init=(int *)realloc(init,n*sizeof(int)); j=n; } ~Pint(){} }; int main(){ int i,j,flag,a,b,t; Pint Pn; while(scanf("%d",&t)&&t){ for(i=0;t>Pn.init[i];i++){ a=Pn.init[i]; b=t-a; flag=Pn.Binary(Pn.init,Pn.j,b); if(flag!=-1){ printf("%d = %d + %d\n",t,a,b); break; } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator