zoukankan      html  css  js  c++  java
  • 整数划分问题

    // 整数s划分展示

    #include <stdio.h>

    void main()

    { int s,i,j,k,t,u; 

      static int a[21][800][21];

      printf("  input s(s<=20):"); scanf("%d",&s);

      a[2][1][1]=1;a[2][1][2]=1;a[2][2][1]=2;

      u=2;

      for(k=3;k<=s;k++)

        { for(j=1;j<=u;j++)

           { a[k][j][1]=1;           

             for(t=2;t<=k;t++)         // 实施在k-1所有划分式前加1操作  

                a[k][j][t]=a[k-1][j][t-1];    

           }

         for(i=u,j=1;j<=u;j++)

           if(a[k-1][j][1]<a[k-1][j][2])   // 若k-1划分式第1项小于第2项  

             { i++;               // 第1项加1为k的第i个划分式的第1项  

               a[k][i][1]=a[k-1][j][1]+1;     

               for(t=2;t<=k-1;t++)

                 a[k][i][t]=a[k-1][j][t];

              }

         i++;a[k][i][1]=k;              // k的最后一个划分式为:k=k  

         u=i;

        }

      for(j=1;j<=u;j++)                // 输出s的所有划分式  

        { printf("%3d: %d=%d",j,s,a[s][j][1]);

          i=2;

          while(a[s][j][i]>0)

            {printf("+%d",a[s][j][i]);i++;}

          printf(" ");

         }

     }

     

    // 整数s划分优化递推设计

    #include <stdio.h>

    void main()

    { int s,i,j,k,t,u; 

      static int a[1600][25];

      printf("input s(s<=24):"); 

      scanf("%d",&s);

      a[1][1]=1;a[1][2]=1;a[2][1]=2;u=2;

      for(k=3;k<=s;k++)

        { for(j=1;j<=u;j++)

            { i=k-1;           

              for(t=i;t>=1;t--)        // 实施在k-1所有划分式前加1操作  

                 a[j][t+1]=a[j][t];

              a[j][1]=1;

            }

          for(t=u,j=1;j<=u;j++)

            if(a[j][2]<a[j][3])          // 若k-1划分式第1项小于第2项  

              { t++;

                a[t][1]=a[j][2]+1;    // 第1项加1  

                i=3;

                while(a[j][i]>0)

                   {a[t][i-1]=a[j][i];i++;}

              }

          t++;a[t][1]=k;                // 最后一个划分式为:k=k  

          u=t;

         }

      for(j=1;j<=u;j++)                // 输出所有u个划分式  

        { printf("%3d: %d=%d",j,s,a[j][1]);

          i=2;

          while(a[j][i]>0)

             {printf("+%d",a[j][i]);i++;}

          printf(" ");

       }

    }

  • 相关阅读:
    asp.net 添加引用类型自动变为GAC
    FPDFCJK.BIN下载(Foxit Reader中/日/韩CJK文字符支持包)
    Failed to access IIS metabase.
    Failed to access IIS metabase.
    判断用户计算机是否安装了sql server
    (Microsoft.Reporting.WebForms.ReportViewer) is not compatible with the type of control (Microsoft.Reporting.WebForms.ReportViewer)
    当你被利用的价值越来越小时,路会越来越窄.
    tomcat 6.0如何配置虚拟目录?tomcat 6.0 不能列目录?
    史上最高科技,Big Data奥运
    基于ping命令的网络故障排查方法
  • 原文地址:https://www.cnblogs.com/liao-pxsoftware15/p/7858362.html
Copyright © 2011-2022 走看看