Data Structures in C 第一章

《Data Structures in C》

机械工业出版社

Ellis Horrowitz, Sartaj Sahni, Susan Anderson-Freed 著

李建中,张岩,李治军译

 

1.2.2 霍纳规则计算多项式

1.2.10 Ackerman函数的递归实现

1.2.11 汉诺塔的递归实现

#include <stdio.h>

#define MAX 20

int main()

{

    float A[MAX];

    float x0;

    float sum; 

    int n;

    

    printf("请输入你要计算的多项式最高次数n的值\n");

    scanf("%d",&n); 

    printf("请输入从a0an的值:\n");

    for(int i = 0; i < n+1; i++)

    {

            scanf("%f",&A[i]);

    }

    printf("请输入x0的值\n");

    scanf("%f",&x0);

    

         sum = A[n] * x0 + A[n-1];

         n--;

    while(n != 0)

    {

            sum = sum * x0 + A[n-1];

            n--;

            

    printf("多项式的和为%f\n",sum);

         return 0;

  

}

 

 

#include <stdio.h>

void hannuota(int,char,char, char);

void mov(char, char);

 

void main()

{

         int n;

         printf("请输入A上的初始盘数:\n");

         scanf("%d",&n);

         hannuota(n,'A','B','C');

}

 

void hannuota(int n, char A, char B, char C)

{

         if(n == 1) mov(A,C);

         else

         {

                   hannuota(n - 1, 'A', 'C', 'B');

                   mov(A, C);

                   hannuota(n - 1, 'B', 'A', 'C');

         }

}

 

void mov(char p ,char q)

{

         printf("move: %c-->%c\n",p,q);

}

posted @ 2011-03-15 12:51  jinmengzhe  阅读(367)  评论(0编辑  收藏  举报