project euler 6~10

本文提供了 Project Euler 中多个编程挑战题的解决方案,包括求平方和差、找到第10001个素数、系列中最大乘积、特殊勾股数三元组以及素数之和的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 6 Sum square difference

在这里插入图片描述

#include <iostream>
#include <cstdio>

using namespace std;

int work(int n) {
    int t1 = 0, t2 = 0;
    for(int i = 1; i <= n; i++) {
        t1 += i * i;
        t2 += i;
    }
    t2 *= t2;
    return t2 - t1;
}

int main(){
    printf("%d\n", work(100));

    return 0;
}

Problem 7 10001st prime

在这里插入图片描述

思路

素数筛法打表

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const int N = 1e6 + 10;

bool isPrime[N];
int prime[N];

int table(int n) {
    int m = (int)sqrt(1.0 * n);
    int tot = 0;
    fill(isPrime, isPrime + n, true);
    isPrime[0] = isPrime[1] = false;
    for(int i = 2; i <= n; i++) {
        if(isPrime[i]) {
            prime[tot++] = i;
            for(int j = i*2; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
    return tot;
}

int main() {
    int tot = table(1000000);
    int n = 10001;
    assert(tot >= n);

    printf("%d\n", prime[n-1]);

    return 0;
}

Problem 8 Largest product in a series

在这里插入图片描述

#include <iostream>
#include <cstdio>

using namespace std;

typedef long long LL;
const int N = 1000 + 10;

LL work() {
    char str[N];
    for(int i = 0; i < 20; i++) {
        scanf(" %s", str + i*50);
    }

    LL ans = 0;
    for(int i = 12; i < 1000; i++) {
        LL val = 1;
        for(int j = i-12; j <= i; j++) {
            val *= str[j]-'0';
        }
        ans = max(ans, val);
    }
    return ans;
}

int main() {
    printf("%lld\n", work());

    return 0;
}

Problem 9 Special Pythagorean triplet

在这里插入图片描述

#include <iostream>
#include <cstdio>

using namespace std;

int work() {
    int n = 1000, ans = -1;
    for(int i = 1; i <= n; ++i) {
        for(int j = i+1; j <= n-i; ++j) {
            int k = n - i - j;
            if(k <= 0) {
                break;
            }
            if(i*i + j*j == k*k) {
                ans = i * j * k;
                break;
            }
        }
        if(ans != -1) {
            break;
        }
    }
    return ans;
}

int main() {
    printf("%d\n", work());

    return 0;
}

Problem 10 Summation of primes

在这里插入图片描述

#include <iostream>
#include <cstdio>

using namespace std;

typedef long long LL;
const int N = 2e6 + 10;

bool isPrime[N];

LL table(int n) {
    fill(isPrime, isPrime + n, true);
    isPrime[0] = isPrime[1] = false;

    LL ans = 0;
    for(int i = 2; i <= n; i++) {
        if(isPrime[i]) {
            ans += i;
            for(int j = i*2; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
    return ans;
}

int main() {
    printf("%lld\n", table(2000000));

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值