NOI:2178 判決素數的個數

題目鏈接


題解:基本題目,掌握判斷素數

#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;
bool set(int t){
    if(t==1) return false;
    if(t==2) return true;
    if(t%2==0) return false;
    int tmp=sqrt(t);
    for(long i=3;i<=tmp;i+=2){
        if(t%i==0)return false;
    }
    return true;
}
int main(){
    int x,y;
    cin>>x>>y;
    if(x>y){
        int tmp=y;
        y=x;
        x=tmp;
    }
    int size=0;
    for(int i=x;i<=y;i++){
        if(set(i))size++;
    }
    cout<<size<<endl;
    return 0;
    
}

發佈了96 篇原創文章 · 獲贊 16 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章