但是最好打印出步骤,比如从5到1,打印出为g->g.
我用二叉树写了一个,但貌似有问题,而且占用内存很大。
#include "stdafx.h"
#include "malloc.h" 
#define MAX 1000
#define NULL 0
int f(int x)
{
    int l;
    l=3*x;
    return (l);
}
int g(int x)
{
    int l;
    l=x/2;
    return (l);
}
struct Node
{
    int getnumber;
    struct Node *lchild,*rchild;
    struct Node *pre;
};
//非递归算法生成二叉树   
int qcreate(int m,int n)   
{      
    struct Node * queue[MAX],*p,*root,*q,*before;   
    int   front=0,rear=0;   
    int x;   
    root=NULL;   
    x=m;  
    p=(struct Node *)malloc(sizeof(struct Node));   
    p->getnumber=x;   
    p->lchild=NULL;   
    p->rchild=NULL;
    p->pre =NULL;
    while(p->getnumber !=n)   
    {   
        p=NULL;   
        if(x!=NULL)   
        {   
            p=(struct Node *)malloc(sizeof(struct Node));   
            p->getnumber=x;   
            p->lchild=NULL;   
            p->rchild=NULL;
            p->pre =NULL;
        }   
        rear++;   
        queue[rear]=p;//入队列   
        if(queue[front++]!=NULL&&p!=NULL)//父结点和子结点都不为空   
        {   
            if(rear==1)//根结点   
                root=p;   
            else   
            {   
                if(rear%2==0)
                {
                    p=before;
                    queue[front++]->lchild=p;//左结点
                    p->pre =before;
                    x=f(x);
                }
                else
                {
                    p=before;
                    queue[front+1]->rchild=p;//右结点
                    p->pre =before;
                    x=g(x);
                }
                if(rear%2==1)   
                    front++;//出队列   
            }   
        }     
    } 
    q=p;
    p=p->pre;
    while(p!=NULL)
    {
        if(p->lchild =q)
        {
            printf("f");
        }
        if(p->rchild =q)
        {
            printf("g");
        }
        q=p;
        p=p->pre ;
    }
    return   0;   
}  
void main()
{
    int start,result;
    scanf("%d%d",&start,&result);
    qcreate(start,result);
}



											
	    

	