PAT (Basic Level) Practise 的C++实现-1007. 素数对猜想

本文介绍了一种基于素数对猜想的程序实现方法,通过计算不超过给定正整数N的所有满足差为2的素数对数量,展示了素数对猜想的实际应用。

让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。

输入格式:每个测试输入包含1个测试用例,给出正整数N。

输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。

输入样例:
20
输出样例:
4



#include <iostream>
#include <vector>
using namespace std;


int main() {
    int i, j,n,f,out,s;
    vector <int>a;
    a.push_back(2);
    cin >> n; 
    out = 0;
    f = 0;
    for (i = 3; i <= n; i++, f = 0) {
        for (j = 0; j < a.size(); j++) {
            if ((i%a[j]) == 0) {
                f = 1;
                break;  
            }
        }
        if (f == 0) {
            a.push_back(i);
            s = a.size();
            if ((a[s-1] - a[s-2]) == 2)
                out++;
        }
    }
    cout << out << endl;
    return 0;

}

学会了vector,没注意到不超过卡了一下。

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project hertzbeat-otel: Compilation fai lure: Compilation failure: [ERROR] 读取C:\Users\DE-LL\.m2\repository\io\opentelemetry\opentelemetry-sdk-logs\1.49.0\opentelemetry-sdk-logs-1.49.0.jar时出错; zip file is empty [ERROR] /F:/Practise/hertzbeat-master/hertzbeat-otel/src/main/java/org/apache/hertzbeat/otel/config/OpenTelemetryLogbackAppenderInstaller.java:[19,1] 无 法访问org.apache.hertzbeat.otel.config [ERROR] ZipException opening "opentelemetry-sdk-logs-1.49.0.jar": zip END header not found [ERROR] /F:/Practise/hertzbeat-master/hertzbeat-otel/src/main/java/org/apache/hertzbeat/otel/config/LogAutoConfiguration.java:[34,8] 无法访问org.apache. hertzbeat.otel [ERROR] ZipException opening "opentelemetry-sdk-logs-1.49.0.jar": zip END header not found [ERROR] /F:/Practise/hertzbeat-master/hertzbeat-otel/src/main/java/org/apache/hertzbeat/otel/config/OpenTelemetryConfig.java:[50,8] 无法访问org.apache.h ertzbeat [ERROR] ZipException opening "opentelemetry-sdk-logs-1.49.0.jar": zip END header not found [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException [ERROR] [ERROR] After correcting the problems, you can resume the build with the command [ERROR] mvn <args> -rf :hertzbeat-otel 解决错误
最新发布
09-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值