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

【小心】 数据可能全是负的 所以要坐一个处理

Posted by WilliamACM at 2012-02-04 17:41:02 on Problem 3978
#include <iostream>
#include <stdio.h>
using namespace std;
const int mm=100002;
bool su[100005]={0};
int a[mm]={0};
void makesu()//做素数表
{
    int i,j,k,m;
    for(i=2;i<=mm/2;i++)
    if(!su[i])
    {
        k=i*2;
        while(k<=mm) su[k]=true,k+=i;
    }
    for(i=2;i<=mm-2;i++)
    if(!su[i]) a[i]=a[i-1]+1;
    else a[i]=a[i-1]; //存放0~i的素数
}
int main()
{
    int l,r;
    makesu();
    scanf("%d%d",&l,&r);
    while(!(l==-1&&r==-1))
    {
        l=max(l,1);//小心此处【关键处理】
        r=max(r,1);//小心此处【关键处理】
        printf("%d\n",a[r]-a[l-1]);
        scanf("%d%d",&l,&r);
    }
   // cout << "Hello world!" << endl;
    return 0;
}

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