以前希望奖的题目,原型是火车调度的问题。不难,模拟一个堆栈,然后再回朔找出所有的可能就可以了。 握着你用了一个不太好的办法,先产生2*n个0。。1串,代表进站和出站,然后判断是否合理,合理着打印。borland pascal7.0通过 program stackstation; const maxn=200; var path:array [1..maxn] of integer; outpath:array[1..maxn] of integer; stack:array[1..maxn] of integer; simu:array[1..maxn] of integer; sp,pointIn,pointOut,m,n:integer; i:integer; function push:boolean; begin push:=true; if pointIn>m then begin push:=false;exit;end; sp:=sp+1; stack[sp]:=path[pointIn]; inc(pointIn); end; function pop:boolean; begin if sp=0 then begin pop:=false;exit;end; outpath[pointOut]:=stack[sp]; sp:=sp-1; inc(pointOut); end; procedure prnt; var i:integer; begin for i:=1 to m do write(chr(ord('a')+outpath[i]-1):4); writeln; end; procedure test; var ret:boolean; begin sp:=0; pointIn:=1;pointOut:=1; for i:=1 to n do if simu[i]=0 then begin ret:=push; if ret=false then exit; end else begin ret:=pop; if ret=false then exit; end; prnt; end; procedure try(i:integer); var j:integer; begin if i=n+1 then begin test;exit;end; for j:=0 to 1 do begin simu[i]:=j; try(i+1); end; end; begin write('m='); readln(m); for i:=1 to m do path[i]:=i; n:=2*m; try(1); end.
#include <memory.h>
#include <string.h>int nWholeSize=0;void ChangeStack(char* pOutStream,char* pSrc,char* pStack,int nSrcSize,int nStackSize,int nStreamSize)
{
char szOutPut[8];
char szSrc[8],szStack[8];
if(nStreamSize==nWholeSize)
{
printf("%s\n",pOutStream);
return ;
}
memset(szOutPut,0,8);
strncpy(szOutPut,pOutStream,nStreamSize);
strncpy(szStack,pStack,nStackSize);
strncpy(szSrc,pSrc,nSrcSize);
if(nStackSize) //pop
{
szOutPut[nStreamSize]=szStack[nStackSize-1]; //add to out stream
ChangeStack(szOutPut,szSrc,szStack,nSrcSize,nStackSize-1,nStreamSize+1);
}
if(nSrcSize) //push
{
szStack[nStackSize]=szSrc[0];
ChangeStack(szOutPut,szSrc+1,szStack,nSrcSize-1,nStackSize+1,nStreamSize);
}
}void main(int argc,char** argv)
{
if(argc<2) return;
nWholeSize=strlen(argv[1]);
if(nWholeSize>=7) nWholeSize=7;
char szSource[8];
memset(szSource,0,8);
strncpy(szSource,argv[1],nWholeSize);
char szStack[8],szOutStream[8];
memset(szOutStream,0,8);
ChangeStack(szOutStream,szSource,szStack,nWholeSize,0,0);
}
#include<stdio.h>
char a[4]={'a','b','c','\0'};
void main()
{
void ok(int,char *); ok(3,a);
}
void ok(int n,char *a)
{
if(n==1){
printf("%s\n",::a);
return;
}
ok(n-1,a+1); char t;
t=a[0];
a[0]=a[n-1];
a[n-1]=t;
ok(n-1,a); t=a[0];
a[0]=a[n-1];
a[n-1]=t;
}
握着你用了一个不太好的办法,先产生2*n个0。。1串,代表进站和出站,然后判断是否合理,合理着打印。borland pascal7.0通过
program stackstation;
const
maxn=200;
var
path:array [1..maxn] of integer;
outpath:array[1..maxn] of integer;
stack:array[1..maxn] of integer;
simu:array[1..maxn] of integer;
sp,pointIn,pointOut,m,n:integer;
i:integer;
function push:boolean;
begin
push:=true;
if pointIn>m then begin push:=false;exit;end;
sp:=sp+1;
stack[sp]:=path[pointIn];
inc(pointIn);
end;
function pop:boolean;
begin
if sp=0 then begin pop:=false;exit;end;
outpath[pointOut]:=stack[sp];
sp:=sp-1;
inc(pointOut);
end;
procedure prnt;
var i:integer;
begin
for i:=1 to m do write(chr(ord('a')+outpath[i]-1):4);
writeln;
end;
procedure test;
var
ret:boolean;
begin
sp:=0;
pointIn:=1;pointOut:=1;
for i:=1 to n do
if simu[i]=0 then begin
ret:=push;
if ret=false then exit;
end
else
begin
ret:=pop;
if ret=false then exit;
end;
prnt;
end;
procedure try(i:integer);
var
j:integer;
begin
if i=n+1 then begin test;exit;end;
for j:=0 to 1 do
begin
simu[i]:=j;
try(i+1);
end;
end;
begin
write('m=');
readln(m);
for i:=1 to m do
path[i]:=i;
n:=2*m;
try(1);
end.
递归算法可考虑f(n+1)= { a, f(n)},{ f(1),a,f(n-1)}, ..., { f(i),a,f(n-i)},... { f(n),a}; 这其中的f(i)中的i指原n个元素的前i个,f(n-i)的n-i指原n个元素的后n-i个。