等差数列程序

#include <iostream>
#include <vector>
#include <set>

using namespace std;

vector<bool> arrayRecord;
set<int> history;
int length = 0;

vector<vector<int>> checkArray(int firstPosition, vector<vector<int>> ans){
    double maxd = (length - firstPosition)/3.0;
    for (int d = 1; d < maxd; d++){
        int secondPosition = firstPosition + d;
        int thirdPosition = firstPosition + 2 * d;
        int fourthPosition = firstPosition + 3 * d;
        if (!arrayRecord[secondPosition] || !arrayRecord[thirdPosition] || !arrayRecord[fourthPosition]){
            continue;
        }
        arrayRecord[secondPosition] = false;
        arrayRecord[thirdPosition] = false;
        arrayRecord[fourthPosition] = false;
        ans.push_back({firstPosition, secondPosition, thirdPosition, fourthPosition});
        int nextFirstPosition = firstPosition + 1;
        while (!arrayRecord[nextFirstPosition]){
            nextFirstPosition++;
            if (nextFirstPosition == length){
                arrayRecord[secondPosition] = true;
                arrayRecord[thirdPosition] = true;
                arrayRecord[fourthPosition] = true;
                return ans;
            }
        }
        vector<vector<int>> nextAns = checkArray(nextFirstPosition, ans);
        if (!nextAns.empty()){
            arrayRecord[secondPosition] = true;
            arrayRecord[thirdPosition] = true;
            arrayRecord[fourthPosition] = true;
            return nextAns;
        }
        ans.pop_back();
        arrayRecord[secondPosition] = true;
        arrayRecord[thirdPosition] = true;
        arrayRecord[fourthPosition] = true;
    }
    return {};
}

void printResult(vector<vector<int>> ans){
    cout << "{";
    for (const auto& group : ans){
        cout << "[";
        for (const auto item : group){
            if(item == group.back()){
                cout << item + 1;
            }else{
                cout << item + 1 << ", ";
            }
        }
        if(group == ans.back()){
            cout << "]";
        }else{
            cout << "], ";
        }
    }
    cout << "}" <<endl;
}

int countNumber(int m){
    length = 4 * m + 2;
    int first;
    int second;
    int count = m * m + m + 1;
    cout << "m 为 " << m << " 时,基础结果共有 " << count << " 组,开始判断特殊情况:" << endl;
    for (int i = 1; i <= m; i++){
        first = 4 * i - 3;
        second = 4 * i;
        if (history.count(first) == 1){
            cout << "去掉元素 " << first + 1 << ", " << second + 1 << " 时,已有成功记录" << endl;
            count++;
            continue;
        }
        arrayRecord[first] = false;
        arrayRecord[second] = false;
        vector<vector<int>> ans = checkArray(0, {});
        if (!ans.empty()){
            cout << "去掉元素 " << first + 1 << ", " << second + 1 << " 时:" << endl;
            printResult(ans);
            history.insert(first);
            count++;
        }
        arrayRecord[first] = true;
        arrayRecord[second] = true;
    }
    cout << "m 为 " << m << " 时,共有 " << count << " 组结果。" << endl << endl;
    return count;
}

int main()
{
    int maxNumber = 8;
    cin >> maxNumber;
    for (int i = 0; i < 4 * maxNumber + 2; i++){
        arrayRecord.push_back(true);
    }
    vector<int> result;
    for (int i = 1; i <= maxNumber; i++){
        result.push_back(countNumber(i));
    }
    cout << "m 值\t总数\t特判数\t理论差距" << endl;
    int index = 1;
    for (const auto item : result){
        cout << index << "\t" << item << "\t" << (item - index * index - index - 1) << "\t" << (index * index + 2 * index + 1 - item) << endl;
        index++;
    }
    return 0;
}

m 为 1 时,基础结果共有 3 组,开始判断特殊情况:
m 为 1 时,共有 3 组结果。

m 为 2 时,基础结果共有 7 组,开始判断特殊情况:
m 为 2 时,共有 7 组结果。

m 为 3 时,基础结果共有 13 组,开始判断特殊情况:
m 为 3 时,共有 13 组结果。

m 为 4 时,基础结果共有 21 组,开始判断特殊情况:
m 为 4 时,共有 21 组结果。

m 为 5 时,基础结果共有 31 组,开始判断特殊情况:
m 为 5 时,共有 31 组结果。

m 为 6 时,基础结果共有 43 组,开始判断特殊情况:
去掉元素 10, 13 时:
{[1, 9, 17, 25], [2, 4, 6, 8], [3, 7, 11, 15], [5, 12, 19, 26], [14, 16, 18, 20], [21, 22, 23, 24]}
去掉元素 14, 17 时:
{[1, 8, 15, 22], [2, 10, 18, 26], [3, 4, 5, 6], [7, 9, 11, 13], [12, 16, 20, 24], [19, 21, 23, 25]}
m 为 6 时,共有 45 组结果。

m 为 7 时,基础结果共有 57 组,开始判断特殊情况:
去掉元素 6, 9 时:
{[1, 3, 5, 7], [2, 8, 14, 20], [4, 10, 16, 22], [11, 13, 15, 17], [12, 18, 24, 30], [19, 21, 23, 25], [26, 27, 28, 29]}
去掉元素 10, 13 时,已有成功记录
去掉元素 14, 17 时,已有成功记录
去掉元素 18, 21 时:
{[1, 2, 3, 4], [5, 12, 19, 26], [6, 14, 22, 30], [7, 8, 9, 10], [11, 13, 15, 17], [16, 20, 24, 28], [23, 25, 27, 29]}
去掉元素 22, 25 时:
{[1, 3, 5, 7], [2, 11, 20, 29], [4, 6, 8, 10], [9, 13, 17, 21], [12, 14, 16, 18], [15, 19, 23, 27], [24, 26, 28, 30]}
m 为 7 时,共有 62 组结果。

m 为 8 时,基础结果共有 73 组,开始判断特殊情况:
去掉元素 2, 5 时:
{[1, 4, 7, 10], [3, 13, 23, 33], [6, 9, 12, 15], [8, 11, 14, 17], [16, 22, 28, 34], [18, 19, 20, 21], [24, 25, 26, 27], [29, 30, 31, 32]}
去掉元素 6, 9 时,已有成功记录
去掉元素 10, 13 时,已有成功记录
去掉元素 14, 17 时,已有成功记录
去掉元素 18, 21 时,已有成功记录
去掉元素 22, 25 时,已有成功记录
去掉元素 26, 29 时:
{[1, 2, 3, 4], [5, 7, 9, 11], [6, 15, 24, 33], [8, 10, 12, 14], [13, 17, 21, 25], [16, 18, 20, 22], [19, 23, 27, 31], [28, 30, 32, 34]}
去掉元素 30, 33 时:
{[1, 7, 13, 19], [2, 12, 22, 32], [3, 4, 5, 6], [8, 9, 10, 11], [14, 15, 16, 17], [18, 21, 24, 27], [20, 23, 26, 29], [25, 28, 31, 34]}
m 为 8 时,共有 81 组结果。

m 值 总数 特判数 理论差距
1 3 0 1
2 7 0 2
3 13 0 3
4 21 0 4
5 31 0 5
6 45 2 4
7 62 5 2
8 81 8 0

 

posted on 2024-06-18 16:23  Eufisky  阅读(17)  评论(0编辑  收藏  举报

导航