【问题标题】:Positive integer N as the sum of positive integers using stack正整数 N 作为使用堆栈的正整数之和
【发布时间】:2016-01-30 13:51:56
【问题描述】:

所以,我正在做这个项目,我们必须写一个正整数 n 作为正整数的 。并且所有解决方案都应该排序,例如:

n = 3
1: 1 1 1 
2: 1 2
3: 3

n = 5 
1: 1 1 1 1 1 
2: 1 1 1 2 
3: 1 1 3 
4: 1 2 2  
5: 2 3 
7: 5

我在纸上得到(我认为)非常好的算法;但是,我很难将其放入代码中。它第一次进入内部 do-while 循环时运行平稳,但随后 'top' 没有从 getTop 函数中获得正确的值。 这是我的 Stack.h 文件

    #include <iostream>
    using namespace std;

    template <class T>
    class Stack
    {
    private:
        // Structure for the stach nodes
        struct StackNode
        {
            T value;          // Value in the node
            StackNode *next;  // Pointer to next node
        };

        StackNode *top;     // Pointer to the stack top
        int count;

    public:
        //Constructor
        Stack(){top = NULL; count = 0;}

        // Destructor
        ~Stack();

        // Stack operations
        bool push(T);
        bool pop(T &);
        bool isEmpty();
        int getCount();
        int getTop();
        void print();

    };


template <class T>
Stack<T>::~Stack()
{
    StackNode *currNode, *nextNode;

    // Position nodePtr at the top of the stack.
    currNode = top;

    // Traverse the list deleting each node.
    while (currNode) //while (currNode != NULL)
    {
        nextNode = currNode->next;
        delete currNode;
        currNode = nextNode;
    }
}


template <class T>
bool Stack<T>::push(T item)
{
    StackNode *newNode; // Pointer to a new node

    // Allocate a new node and store num there.
    newNode = new StackNode;
    if (!newNode)
        return false;
    newNode->value = item;

    // Update links and counter
    newNode->next = top;
    top = newNode;
    count++;

    return true;
}


template <class T>
bool Stack<T>::pop(T &item)
{
    StackNode *temp; // Temporary pointer

    // empty stack
    if (count == 0)
        return false;

    // pop value off top of stack
    item = top->value;
    temp = top->next;
    delete top;
    top = temp;
    count--;

    return true;
}

template <class T>
bool Stack<T>::isEmpty()
{
    return count == 0;
}


template <class T>
int Stack<T>::getCount()
{
    return count;
}


template <class T>
int Stack<T>::getTop() 
{
    if (this->top->next == NULL)
    {
        cout << "EXCEPTION" << endl;
    }
    else
    {
        return this->top->next->value;
    }
    //return top->value;
}

template <class T>
void Stack<T>::print()
{
    StackNode *newNode;
    newNode = top;
    for (int i = count; i > 0; i--)
    {
    cout << newNode -> value;
    cout << " ";
    newNode = newNode -> next;      
    }

}

#endif

这是我的 main.cpp

#include <iostream>
#include "Stack.h"

using namespace std;

int main()
{
    int input = 0;
    int counter = 0;
    int pop1 = 0;
    int pop2 = 0;
    int top;
    int CurrSum = 0;
    Stack<int> *stack = new Stack<int>;
    Stack<int> *tmpStack = new Stack<int>;

    cout << "Please enter a integer: " << endl;
    cin  >> input;
    while(input < 0)
    {
        cout << "Please enter a POSITIVE integer: " << endl;
        cin  >> input;
    }

    if ( input != 0)
    {
        counter ++;
        for (int n = 1; n <= input; n ++)
        {
            stack -> push(1);
        }

     do
     {


        do
        {
            top = stack->getTop();
            pop1 = stack->pop(top);
            top = stack->getTop();
            pop2 = stack->pop(top);

            //increment the pop2 and push it back
            stack->push(pop2 + 1);

            top = stack->getTop();

            CurrSum = CurrSum - pop1 - pop2 + top;

            if (CurrSum < input)
            {
                stack->push(top);
                CurrSum = CurrSum + top;
            }
            else if (CurrSum > input)
            {
                pop1 = stack->pop(top);
                top = stack->getTop();
                pop2 = stack->pop(top);

                CurrSum = CurrSum - pop1 - pop2 + top;
            }

        }while (CurrSum == input);

        cout << counter << ": ";
        stack->print();
        top = stack->getTop();

     }while (top != input);

        system("pause");
    }
    else
        cout << "Thank you for using this program. Bye!" << endl;
    return 0;
}

【问题讨论】:

  • stack -&gt; push(1); 你的意思是在这里写stack -&gt; push(n);吗?
  • @πάνταῥεῖ 我想他写了他想要写的东西,但他忘了在for循环之后将CurrSum设置为n。但是我必须承认我并没有完全理解那个算法......
  • Stack&lt;T&gt;::top() 返回顶部之后的元素,修复此问题会使您的代码看起来有缺陷。这是一些输出: [4]: 2, 1, 1, 1, [3]: 2, 1, 1, [2]: 2, 1, [1]: 2,
  • 我不明白你的例子,n=5。我认为这个想法是总是对最后两个元素求和,但是你会从3: 1 1 34: 1 2 2,我不明白。究竟是什么规则?
  • @alexlop 我确实忘记在循环后将 CurrSum 设置为 n,谢谢

标签: c++ pointers integer sum stack


【解决方案1】:

首先,您不需要为自己编写 Stack 类,它已经为您完成了:它被称为 std::stack,并且完全符合您的需要。其次,你真的不需要动态分配它,无论你使用自己的Stack还是std::stack,虽然它在功能上是相同的,但动态分配效率较低。

如果这仍然不起作用,您现在可以确定您的算法是错误的(我没有检查它)。由于这是一个典型的递归问题,我会尝试递归编写它(现在你根本不需要 Stack 类),因为当你递归编写它们时,像这样的递归算法通常更容易理解。稍后您可以根据需要迭代地重写它,一旦您了解出了什么问题。

【讨论】:

  • 我知道这个 stl 库,但我还是想通过我自己设置的这些函数来解决问题
  • 它不能被视为答案,因为它不能回答/解决给定的问题。应该是评论。
  • @AlexLop。评论太长了,不是吗?
  • @petersohn 太长的评论仍然不是答案,可以分成 2 条……所以我建议你把它移到评论中
猜你喜欢
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
相关资源
最近更新 更多