project euler 11~15

Problem 11

在這裏插入圖片描述

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 20 + 10;

int work(int n) {
    int arr[N][N];
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            scanf("%d", &arr[i][j]);
        }
    }

    int ans = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            if(i + 4 <= n) {
                ans = max(ans, arr[i][j]*arr[i+1][j]*arr[i+2][j]*arr[i+3][j]);
            }
            if(j + 4 <= n) {
                ans = max(ans, arr[i][j]*arr[i][j+1]*arr[i][j+2]*arr[i][j+3]);
            }
            if(i + 4 <= n && j + 4 <= n) {
                ans = max(ans, arr[i][j]*arr[i+1][j+1]*arr[i+2][j+2]*arr[i+3][j+3]);
            }
            if(i + 4 <= n && j >= 3) {
                ans = max(ans, arr[i][j]*arr[i+1][j-1]*arr[i+2][j-2]*arr[i+3][j-3]);
            }
        }
    }
    return ans;
}

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

    return 0;
}

Problem 12 Highly divisible triangular number

在這裏插入圖片描述

思路

計算一個三角形數的約數時,需要用O(n)O(\sqrt{n})的算法

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

using namespace std;

bool check(int val, int n) {
    int cnt = 0;
    int m = (int)sqrt(1.0 * val);
    for(int i = 1; i <= m; ++i) {
        if(val % i == 0) {
            cnt += (val/i == i) ? 1 : 2;
        }
    }

    return cnt >= n;
}

int work(int n) {
    int val = 0, cnt = 1;
    while(true) {
        val += cnt;
        if(check(val, n)) {
            break;
        }
        ++cnt;
    }

    return val;
}

Problem 13 Large sum

在這裏插入圖片描述

思路

高精度加法求和,取結果的前1010

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long LL;
const int N = 100;

void add(char* s1, char* s2, char* sum) {
    int len1 = strlen(s1), len2 = strlen(s2);
    reverse(s1, s1 + len1);
    reverse(s2, s2 + len2);

    int len = 0;
    int carry = 0;
    for(int i = 0; i < len1 || i < len2; i++) {
        carry += i<len1 ? s1[i]-'0' : 0;
        carry += i<len2 ? s2[i]-'0' : 0;
        sum[len++] = carry%10 + '0';
        carry /= 10;
    }
    if(carry) {
        sum[len++] = carry + '0';
        sum[len] = '\0';
    }

    reverse(s1, s1 + len1);
    reverse(s2, s2 + len2);
    reverse(sum, sum + len);
}

LL work() {
    char sum[N], s1[N], s2[N];
    memset(sum, 0, sizeof(sum));
    memset(s2, 0, sizeof(s2));
    for(int i = 0; i < 100; i++) {
        scanf(" %s", s1);
        add(s1, s2, sum);
        memcpy(s2, sum, sizeof(sum));
    }

    LL ans = 0;
    for(int i = 0; i < 10; i++) {
        ans = ans*10 + sum[i]-'0';
    }
    return ans;
}

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

    return 0;
}

Problem 14 Longest Collatz sequence

在這裏插入圖片描述

思路

直接搜索求每個數的序列長度,取序列最長的那個數字,但搜索過程中要把已經搜索過的數字的序列長度緩存起來,可以減少大量的無效搜索。注意搜索過程的中間數值會爆intint

#include <iostream>
#include <cstdio>

using namespace std;

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

int step[N];

int dfs(LL val) {
    if(val < N && step[val] != 0) {
        return step[val];
    }

    int temp = dfs((val & 1) ? val*3+1 : val/2) + 1;
    if(val < N) {
        step[val] = temp;
    }
    return temp;
}


int work(int n) {
    memset(step, 0, sizeof(step));
    step[1] = 1;
    int ans = 0, maxStep = 0;
    for(int i = 1; i <= n; i++) {
        int temp = dfs(i);
        if(maxStep < temp) {
            maxStep = temp;
            ans = i;
        }
    }
    return ans;
}

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

    return 0;
}

Problem 15 Lattice paths

在這裏插入圖片描述

思路

實際上是212121*21的點陣,簡單動態規劃,狀態轉移方程:dp[i][j]=dp[i1][j]+dp[i][j1]dp[i][j] = dp[i-1][j] + dp[i][j-1]

#include <iostream>
#include <cstdio>

using namespace std;

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

LL work(int n) {
    LL dp[N][N];
    memset(dp, 0, sizeof(dp));
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(i == 1 && j == 1) {
                dp[i][j] = 1;
            } else {
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
    }
    return dp[n][n];
}

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

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章