美文网首页
基础算法

基础算法

作者: 喵个酱 | 来源:发表于2019-03-09 19:12 被阅读0次

    一、递推算法

    1、顺推算法

    兔子的繁殖过程

    #include <stdio.h>

    #define NUM 13

    int main()

    {

        int i;

        //初始月份和第一个月的兔子总数为1

        long fib[NUM]={1,1};

        for(i=2;i<NUM;i++)

        {

             //本月的兔子=上两个月兔子之和

            fib[i]=fib[i-1]+fib[i-2];

        }

        for(i=0;i<NUM;i++)

        {

            printf("%d月兔子总数:%d\n",i,fib[i]);

        }

        return 0;

    }

    2、顺推实例

    最后一个月连本带息取出1000

    #include <stdio.h>

    #define FETCH 1000//最后取出1000

    #define RATE 0.0171//利率 /100

    int main()

    {

        //便于计算

        double corpus[49];

        int i;

        corpus[48]=(double)FETCH;

        for(i=47;i>0;i--)

        {

            corpus[i]=(corpus[i+1]+FETCH)/(1+RATE/12);

        }

        for(i=48;i>0;i--)

        {

            printf("第%d月末本利合计:%.2f\n",i,corpus[i]);

        }

        return 0;

    }

    二、枚举(穷举)算法

    1、算法描述题

                   算

    ----------------

    题题题题题

    #include <stdio.h>

    int main()

    {

        int i1,i2,i3,i4,i5;

        long multi,result;

        //算

        for(i1=1;i1<=9;i1++)

        {

            //法

            for(i2=0;i2<=9;i2++)

            {

                //描

                for(i3=0;i3<=9;i3++)

                {

                    //述

                    for(i4=0;i4<=9;i4++)

                    {

                        //题

                        for(i5=0;i5<=9;i5++)

                        {

                            multi=i1*10000+i2*1000+i3*100+i4*10+i5;

                            result=i5*100000+i5*10000+i5*1000+i5*100+i5*10+i5;

                            if(multi*i1==result)

                            {

                                printf("\n%5d%2d%2d%2d%2d\n",i1,i2,i3,i4,i5);

                                printf("X%12d\n",i1);

                                printf("_____________________\n");

                                printf("%3d%2d%2d%2d%2d%2d\n",i5,i5,i5,i5,i5,i5);

                            }

                        }

                    }

                }

            }

        }

    return 0;

    }

    2、实例:填运算符

    5 5 5 5 5=5

    #include <stdio.h>

    int main()

    {

        int j,i[5];//循环变量,数组i表示4个运算符

        int sign;

        int result;

        int count=0;

        int num[6];

        float left,right;

        char oper[5]={' ','+','-','*','/'};//运算符,第0个元素不用

        printf("请输入5个数:");

        for(j=1;j<=5;j++)

            scanf("%d",&num[j]);

        printf("请输入结果:");

        scanf("%d",&result);

        for(i[1]=1;i[1]<=4;i[1]++)//1:+,2:-,3:*,4:/

        {

            //排除除号后跟0的情况

            if(i[1]<4||num[2]!=0)

            {

            for(i[2]=1;i[2]<=4;i[2]++)

            {

            if(i[2]<4||num[3]!=0)

            {

            for(i[3]=1;i[3]<=4;i[3]++)

            {

            if(i[3]<4||num[4]!=0)

            {

                for(i[4]=1;i[4]<=4;i[4]++)

                {

                    if(i[4]<4||num[5]!=0)

                    {

                        //左边为0

                        left=0;

                        //右边为第一个数

                        right=num[1];

                        //加法

                        sign=1;

                        for(j=1;j<=4;j++)

                        {

                            //第j个运算符

                           switch(oper[i[j]])

                           {

                                case '+':

                                    left=left+sign*right;

                                    sign=1;

                                    right=num[j+1];

                                    break;

                                case '-':

                                    left=left+sign*right;

                                    sign=-1;

                                    right=num[j+1];

                                    break;

                                case '*':

                                    right=right*num[j+1];

                                    break;

                                case '/':

                                    right=right/num[j+1];

                                    break;

                            }

                        }

                        if(left+sign*right==result)

                        {

                            count++;

                            printf("%3d:",count);

                            for(j=1;j<=4;j++)

                            printf("%d%c",num[j],oper[i[j]]);

                            printf("%d=%d\n",num[5],result);

                        }

                      }

                    }

             }

            }

            }

            }

            }

        }

    if(count==0)

    printf("没有符合要求的方法!\n");

    return 0;

    }

    相关文章

      网友评论

          本文标题:基础算法

          本文链接:https://www.haomeiwen.com/subject/tlqepqtx.html