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