c++ - 递归函数中的类变量访问

标签 c++ class variables recursion

对于介绍程序,我们被要求构建一个程序,可以找到给定大小的每个可能的工作魔方。我在从递归函数中修改类变量时遇到问题。每当我尝试的数字组合产生一个幻方时,我都会尝试增加找到的幻方数。

更具体地说,我试图在函数 recursiveMagic() 中修改 numSquares。在该特定行设置断点后,变量 numSquares 不会更改,即使我正在递增它。我认为这与递归有关,但是我不确定。如果你想提供一些建议,我很感激。

//============================================================================
// Name        : magicSquare.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

/**
 * MagicSquare
 */
class MagicSquare {
private:
    int magicSquare[9];
    int usedNumbers[9];
    int numSquares;
    int N;
    int magicInt;
public:

    MagicSquare() {
        numSquares = 0;
        for (int i = 0; i < 9; i++)
            usedNumbers[i] = 0;
        N = 3; //default is 3
        magicInt = N * (N * N + 1) / 2;
    }

    MagicSquare(int n) {
        numSquares = 0;
        for (int i = 0; i < 9; i++)
            usedNumbers[i] = 0;
        N = n;
        magicInt = N * (N * N + 1) / 2;
    }

    void recursiveMagic(int n) {
        for (int i = 1; i <= N * N + 1; i++) {
            if (usedNumbers[i - 1] == 0) {
                usedNumbers[i - 1] = 1;
                magicSquare[n] = i;
                if (n < N * N)
                    recursiveMagic(n + 1);
                else {
                    if (isMagicSquare()) {
                        numSquares++; //this is the line that is not working correctly
                        printSquare();
                    }
                }
                usedNumbers[i - 1] = 0;
            }
        }
    }
    //To efficiently check all rows and collumns, we must convert the one dimensional array into a 2d array
    //since the sudo 2d array looks like this:
    //        0 1 2
    //        3 4 5
    //        6 7 8
    //the following for-if loops convert the i to the appropriate location.

    bool isMagicSquare() {
        for (int i = 0; i < 3; i++) {
            if ((magicSquare[i * 3] + magicSquare[i * 3 + 1] + magicSquare[i * 3 + 2]) != magicInt) //check horizontal
                return false;
            else if ((magicSquare[i] + magicSquare[i + 3] + magicSquare[i + 6]) != magicInt) // check vertical
                return false;
        }
        if ((magicSquare[0] + magicSquare[4] + magicSquare[8]) != magicInt)
            return false;
        if ((magicSquare[6] + magicSquare[4] + magicSquare[2]) != magicInt)
            return false;
        return true;
    }

    /**
     * printSquare: prints the current magic square combination
     */
    void printSquare() {
        for (int i = 0; i < 3; i++)
            cout << magicSquare[i * 3] << " " << magicSquare[i * 3 + 1]
                << " " << magicSquare[i * 3 + 2] << endl;
        cout << "------------------" << endl;
    }

    /**
     * checkRow: checks to see if the current row will complete the magic square
     * @param i - used to determine what row is being analyzed
     * @return true if it is a working row, and false if it is not
     */
    bool checkRow(int i) {
        i = (i + 1) % 3 - 1;
        return (magicSquare[i * 3] + magicSquare[i * 3 + 1] + magicSquare[i * 3 + 2]) == magicInt;
    }

    int getnumSquares() {
        return numSquares;
    }
}; //------End of MagicSquare Class-----

int main() {
    MagicSquare square;
    cout << "Begin Magic Square recursion:" << endl << "------------------"
            << endl;
    square.recursiveMagic(0);
    cout << "Done with routine, returned combinations: " << square.getnumSquares() << endl;
    return 0;
}

最佳答案

数组正在被覆盖,导致覆盖 numSquares 字段。

class MagicSquare {
private:
    int magicSquare[9];
    int usedNumbers[9];

更改

class MagicSquare {
private:
    int magicSquare[10];
    int usedNumbers[10];

同样在你的初始化程序中,循环说 < 9 但你想说的是 < 10。或者只使用 memset 更适合这个目的。

关于c++ - 递归函数中的类变量访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14925065/

相关文章:

c++ - 计算文本文件(C++)中的单词

java - C++中接口(interface)的匿名实现

c++ - 在 C++ 中递增 char 指针

python - 自定义变量字段到python日志记录

swift - 从另一个类 Swift 访问变量

c++ - 在行星上实现四叉树地形(Geomipmapping)

c++ - 没有重载函数 "search"的实例与参数列表匹配

c# - C# 中的类和列表 <> 引用类型

javascript - 如何获取json变量的关键字?

ios - 使用字符串变量设置导航栏标题