尿检粘液丝阳性:谁有PASCAL语言的回朔算法教程?急求!!!

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/01 13:54:31
最好详细一点。

给你一个C++的看一下

#include <iostream>
#include <cstdlib>
#include <stack>
using namespace std;

int
number (int n);

int
main ()
{
cout << "Please input a number:";
int n;
cin >> n;
if (n < 1)
{
cout << "Error!" << endl;
system ("Pause");
return 1;
}

cout << "The result is: " << number (n) << endl;

system ("Pause");
return 0;
}

struct foo
{
int n; // the number
int m; // the m
int i; // the i
int v; // return value
bool b;
};

int
number (int n)
{
stack<foo> s;
int v = 0;
foo f;
f.n = n;
f.m = n;
f.i = n;
f.v = 0;
f.b = false;

s.push (f);
while (s.size() > 0)
{
foo f = s.top();
cout << f.n << " " << f.m << " " << f.i << endl;
if (f.b)
{
s.top().v += v;
s.top().i--;
v = 0;
s.top().b = false;
}
else if (f.n == 0)
{
v = 1;
s.pop ();
}
else if (f.n < 0)
{
v = 0;
s.pop ();
}
else if (f.m == 1)
{
v = 1;
s.pop ();
}
else if (f.i == 0)
{
v = f.v;
s.pop();
}
else if (f.i > 0)
{
foo next;
next.n = f.n - f.i;
next.m = f.i;
if (next.n < next.m)
next.m = next.n;
next.i = f.i;
next.v = 0;
next.b = false;

s.top().b = true; // wait for return value

s.push (next);
}
}
return v;
}