0

我希望我不会在这个网站上受到打击。我是一个新手程序员,对这个概念有很大的困难。我的任务是记忆斐波那契数列。我认为我掌握了一个概念,但当然实施指南如下,这让我感到困惑。

typedef struct Memo
{
// dynamically allocated HugeInteger array to store our Fibonacci numbers
struct HugeInteger *F;

// the current length (i.e., capacity) of this array
int length;
} Memo;

typedef struct HugeInteger
{
// a dynamically allocated array to hold the digits of a huge integer
int *digits;

// the length of the array (i.e., number of digits in the huge integer)
int length;
} HugeInteger;

正如您所看到的那样,斐波那契数不会是简单的“int”,而是它们将是存储在 int 数组中的数字,例如 int - 134528 将在一个数组中,如 int someNumber[6] = {1,3, 4,5,2,8}

我创建了这样的备忘录:

Memo *createMemo(void)
{
/*
Description: Create and initialize a Memo struct:
1. Dynamically allocate space for a new Memo struct.

2. Within the Memo struct, dynamically allocate an array of INIT_MEMO_SIZE number of
HugeInteger structs. INIT_MEMO_SIZE is defined in Fibonacci.h.

Note: This is an array of actual structs, not pointers to structs. So, you cannot set     the
individual elements of this array to NULL, and you cannot free() them.

3. Once the array is created, initialize memo→length appropriately.

4. Make two calls to HugeInit() to initialize F[0] and F[1] (your two Fibonacci base
cases) within the Memo struct.

5. For all remaining F[i], initialize the digits field to NULL and the length field to 0     to
indicate that F[i] has not yet been memoized.

Panic: If any calls to malloc() fail within this function, call panic() (defined in
HugeInteger.c) with the following string argument: “ERROR: out of memory in
createMemo()\n”.

Returns: A pointer to the new Memo struct.
*/
int i;

//Dynamically allocate new Memo
Memo *newMemo = malloc(sizeof(Memo));

        //Check if malloc failed
        if(newMemo == NULL)
            panic("ERROR: out of memory in createMemo()\n");

//Malloc a newMemo->F struct
newMemo->F = malloc(sizeof(HugeInteger *) * INIT_MEMO_SIZE);

        //Check if malloc failed
        if(newMemo->F == NULL)
            panic("ERROR: out of memory in createMemo()\n");

//Initialize the length of the Memo
newMemo->length = INIT_MEMO_SIZE;

//Make two calls to HugeInit to initialize F[0] and F[1] base cases
HugeInit(&newMemo->F[0], 0);
HugeInit(&newMemo->F[1], 1);

//Initialize the rest of F[i] to NULL & length to 0 so I now it's not memoized
for(i=2; i<INIT_MEMO_SIZE; i++)
    {
    newMemo->F[i].digits = NULL;
    newMemo->F[i].length = 0;
    }

//Return the newly created Memo
return newMemo;
}

创建新备忘录后,我现在输入斐波那契函数,这就是我的大脑失败的地方。我像这样实现了我的斐波那契函数: struct HugeInteger *fib(int n, Memo *memo)

{
/*
Description:
Implement a recursive solution that checks whether F(n) has already been memoized and,     if so,
returns F(n) without making any recursive calls. 

1. If n exceeds the bounds of the array in memo, call expandMemo(). For more information
on what parameters to pass to expandMemo(), refer to its function description above.

3. In order to compute and memoize F(n), you must call the HugeAdd() function that is
defined in HugeInteger.c. The function requires F(n - 1) and F(n - 2) to be memoized
already. HugeAdd() will memoize F(n) if you call it correctly (i.e., it will store the     result
in memo).

Returns: The address of the struct within memo that holds F(n). If memo is NULL or n is     less than
0, return NULL without performing any recursive calls and without calling expandMemo().
*/
int i;

//Handle for negative numbers
if(n < 0 || memo == NULL)
    return NULL;

//Handle for index out of bounds
if(n > memo->length)
    expandMemo(memo, n);

//Check Memory if F[n] already exists and return it
if(memo->F[n].digits != NULL)
    return &memo->F[n];

//return the base cases;
if(n < 2)
    return &memo->F[n];
else
{
    //My thought process was to make two recursive calls and then use the provided
 HugeAdd function to add the information together. HOWEVER the HugeAdd function is a 
void function so it does it's thing and returns nothing. basically it takes the two
arrays and adds them index by index like you would on paper. 
The first argument of Huge add is the first HugeInteger to be added, then the second 
argument is the second HugeInteger to be added, and the third argument is the
HugeInteger where you want the result. second problem I see is that temp1 is never
being malloc'd or initialized so as the fibonacci series grows temp1 will need to be
malloc'd accordingly and then it will need to be filled accordingly. I am lost 
completely as to how to implement this. 

    HugeInteger *temp1 = fib(n-1, memo);
    HugeInteger *temp2 = fib(n-2, memo);

    HugeAdd(&temp1, &temp2, &memo->F[n]);
}

return &memo->F[n];

}

我完全不知道如何将斐波那契数放入各自的数组槽中,然后将它们推送到 HugeAdd 函数。我列出的功能无法更改,我只能创建辅助功能来辅助。

我只是想要洞察力,我知道我必须像其他人一样独自跋涉编程地狱的深处,我只需要一点指导,这样我就可以一次停止编写垃圾代码 6 个小时,结果却一无所获。

4

1 回答 1

0

正如我在评论中指出的那样,您应该尝试使用HugeIntegerfirst 的存根实现来调试程序。一旦你确信你的斐波那契例程的逻辑是有效的,那么你可以尝试集成真正的HugeInteger实现。为了帮助您入门,这里有一个简单的存根:

typedef struct HugeInteger {
    unsigned long long digits;
    int length;
} HugeInteger;

void HugeInit (HugeInteger *h, unsigned long long val) {
    h->digits = val;
    h->length = 1;
}

void HugeAdd (const HugeInteger *a, const HugeInteger *b, HugeInteger *c) {
    assert(a->length);
    assert(b->length);
    assert(!c->length);
    c->digits = a->digits + b->digits;
    c->length = 1;
}

void HugePrint (const HugeInteger *a) {
    assert(a->length);
    printf("%llu\n", a->digits);
}

另一个指针。在您的createMemo()例程中,您没有F正确分配数组:

//Malloc a newMemo->F struct
newMemo->F = malloc(sizeof(HugeInteger *) * INIT_MEMO_SIZE);

F成员Memo是 a HugeInteger *,因此,您要改为分配sizeof(HugeInteger) * INIT_MEMO_SIZE

于 2013-06-12T01:53:38.587 回答