zoukankan      html  css  js  c++  java
  • Dynamic Programming

     1 public static void main(String[] args) {
     2         //give N, find the number of different ways to write N as the sum of 1, 3, 4
     3         // 1. subproblem; 2. relation; 3. the begin data
     4         // 1. d[n]: n value and the ways d[n]
     5         // 2. d[n] = d[n-1] + d[n-3] + d[n-4] 
     6         // 3. d[0],d[1],d[2],d[3]
     7         int n = 5;
     8         
     9         if (n <= 3 && n >= 0) {
    10             switch (n) {
    11             case 0:
    12                 System.out.println("Ways: "+1);
    13                 break;
    14             case 1:
    15                 System.out.println("Ways: "+1);
    16                 break;
    17             case 2:
    18                 System.out.println("Ways: "+1);
    19                 break;
    20             case 3:
    21                 System.out.println("Ways: "+2);
    22                 break;
    23             default:
    24                 break;
    25             }
    26             
    27         } else if (n >= 4) {
    28             //core
    29             int[] ways = new int[n+1];
    30             ways[0]=1;ways[1]=1;ways[2]=1;ways[3]=2;
    31             for (int i = 4; i <= n; i++) {
    32                 ways[i] = ways[i-1] + ways[i-3] + ways[i-4];
    33             }
    34             System.out.println("Ways: " + ways[n]);
    35         } else {
    36             System.out.println("N should be >= 0!");
    37         }
    38         
    39     }
    清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
  • 相关阅读:
    路由守卫
    this.$nextTick() 的一些理解
    3d饼图
    element ui 可编辑的表格
    vue 路由传参
    vue+element ui 时间格式化
    element ui 选择期 传对象
    数据结构学习第十天
    数据结构学习第九天
    数据结构学习第八天
  • 原文地址:https://www.cnblogs.com/hello-yz/p/4662881.html
Copyright © 2011-2022 走看看