Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我这个125MS 有一些主要的地方可以参考下。

Posted by lsz2013 at 2013-11-02 19:52:11 on Problem 2262
In Reply To:贴个代码吧,375MS,提交了三次,速度丝毫没变,如果有更快的,欢迎分享 Posted by:luo223 at 2013-09-12 22:27:04
Source Code

Problem: 2262		User: lsz2013
Memory: 4088K		Time: 125MS
Language: C		Result: Accepted
Source Code
#include<stdio.h> 
#include<math.h>
#define MAX 1000001 
int prime[MAX]={0};  
void sushu() 
{ 
    int i,j; 
    for(i=2;i<sqrt(MAX);i++) //这里必须要sqrt(MAX),若是MAX报错!
	{
		if(!prime[i]) 
		{
            for(j=i*i;j<MAX;j+=i) 
			{
				prime[j]=1;
			}
		}
	}
} 
int main() 
{ 
    int n,i; 
    sushu(); 
    while (scanf("%d",&n)&&n)  
    { 
        for(i=3;i<=n/2;i+=2)  
		{
			if(!prime[i]&&!prime[n-i]) 
			{
                printf("%d = %d + %d\n", n, i, n-i); 
                break; 
			}
		}
    } 
    return 0; 
}

本题大致有两种思路:(1)暴力的素数判断法。(2)先用筛选素数的方法筛选出小于1000000的素数,之后就直接进行查表就行了。 第一种效率要低。对于第二种要注意将数组的定义为全局的,否则不让定义那么大的。  唉,我也是磕磕绊绊的submit过来的,注意= 一定!

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator