文章

43

粉丝

24

获赞

293

访问

6.6k

头像
素数判定 题解:素数定义+整体思路
P1102 兰州大学机试题
发布于2025年2月12日 10:56
阅读数 129

#include <bits/stdc++.h>
using namespace std;
//素数是指那些大于1,并且只能被1和它自己整除的数
//思路: 判断一个数是不是素数,即用它除以小于其根号的数[2-sqrt(i)],若能整除 则说明非素数 
int main(){
    int a,b;
    while(cin>>a>>b) {
        int first=min(a,b);
        int last=max(a,b);
        int ans=0;
        for(int i=first;i<=last;i++){//对每个数进行判定 
            int flag=0;//0代表为素数 
            for(int j=2;j<=sqrt(i);j++){
                if(i%j==0){
                    flag=1;
                    break;
        ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发