您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

题解 CF755A 【PolandBall and Hypothesis】

bubuko 2022/1/25 20:09:56 其他 字数 1272 阅读 941 来源 http://www.bubuko.com/infolist-5-1.html

直接从1开始枚举不就行了... 思路如下: 1.先定义一个判断是不是质数的函数 ...

直接从1开始枚举不就行了...

思路如下:

1.先定义一个判断是不是质数的函数

int pd(int n)
{
    if(n==1)return true;
    if(n==2)return false;
    for(int i=2;i*i<=n;i++)
        if(n%i==0)return true;
    return false;
}

2.从1开始枚举,可以直接使用
for(int i=1;;i++)
进行枚举

3.判断i是否满足要求,调用函数,如果满足,就直接输出i并且break或return 0

for(int i=1;;i++)
    {
        if(pd(n*i+1))
        {
            cout<<i;
            return 0;
        }
    }
#include <bits/stdc++.h>
using namespace std;
int pd(int n)
{
    if(n==1)return true;//特判
    if(n==2)return false;
    for(int i=2;i*i<=n;i++)
        if(n%i==0)return true;
    return false;
}//判断是不是素数
int main()
{
    long long n;
    cin>>n;
    for(int i=1;;i++)
    {
        if(pd(n*i+1))//按照题目里的公式判断
        {
            cout<<i;
            return 0;//找到了,return
        }
    }//枚举
    return 0;
}

题解 CF755A 【PolandBall and Hypothesis】

原文:https://www.cnblogs.com/tearing/p/12371552.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶