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<iostream> #include<math.h> using namespace std; #define MAX 10001 int prime[MAX]={0};//素数表,为0的是素数,1的为非素数 void getprime()//求出素数表 { prime[0]=1; prime[1]=1;//1不是素数 int i,stop=100,j; for(i=2;i<=stop;i++) { if(prime[i]==0) { j=i*2; while(j<=MAX) { prime[j]=1; j=j+i; } } } } int digit_sum(int p) { int sum=0; while(p>=10) { sum=sum+p%10; p=p/10; } sum=sum+p; return sum; } int fenjie(int p) { int i,now=p,sum=0,stop; stop=sqrtf((float)p); for(i=2;i<=stop;i++) { if(prime[i]==0) { while(now%i==0) { now=now/i; sum+=i; } if(now==1) break; } } if(now!=1) sum=sum+digit_sum(now); return sum; } int main() { getprime(); int i; int begin; while(scanf("%d",&i)) { if(i==0) break; for(begin=i+1;;begin++) { //检查是否为素数 int j,j_stop; bool isprime=true; j_stop=sqrtf((float)begin); for(j=2;j<=j_stop;j++) { if(begin%j==0) { isprime=false; break; } } if(isprime) continue; else { int fen_sum=fenjie(begin); int dig_sum=digit_sum(begin); if(fen_sum==dig_sum) { printf("%d\n",begin); 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