【问题标题】:Selling Apartments Recursive Definition销售公寓递归定义
【发布时间】:2016-06-08 19:47:15
【问题描述】:

我的任务如下:

一家建筑公司已完成一栋带有 A 公寓的新建筑 这些都可以在市场上买到。有 B 个潜在买家 这些公寓。

给定一个买家 b 和两个数组 s; p,值 s[b] 对应于 买家 b 想购买的公寓数量,p[b] 对应 买方愿意为公寓支付的金额。的目标 公司将确定它可以通过以下方式获得的最大利润 将公寓出售给合适的买家。请注意,并非所有 公寓需要出售,买方要么得到所有公寓 他要求或没有。

示例:假设有 8 套公寓待售和 4 套潜力 买家。每个买家想要购买的公寓数量和 他们愿意支付的价格在数组中可用:s = [4; 1个; 2; 3] 和 p = [8; 1个; 10个; 9],分别。最大的保护 可以达到的是20。 3.1 [3 分] 假设 P(b;a) 是考虑到前 b 个买家的要求和当 a 公寓可供出售。给出 P(b;) 的递归定义 一)。

你们中有人知道如何递归解决这个问题吗?

谢谢

【问题讨论】:

  • 我过去解决了其他动态规划和递归问题(斐波那契、在不同年份销售不同的葡萄酒等),但在这里我似乎找不到对我有意义的最终条件。 ..当没有更多的公寓,没有更多的买家时,我试图中止,但是递归调用无法编码......

标签: algorithm recursion dynamic-programming


【解决方案1】:

1.) 形成一个包含 (p/s) 值的数组,如 [2,2,2,2,1,5,5,3,3,3]。我在这里假设 (p/s) 的任何值都不会产生十进制数。

2.)现在,您可以将此问题简化为 k=8(待售公寓数量)的 K 组合问题。

3.) 打印出所有 k=8 且 n 为数组中的值的组合,您将看到使用此方法得到的最大值为 20。

【讨论】:

    【解决方案2】:

    认为这可能行得通。

    class ApartmentBuyer{
    
        public static void main(String[] args){
            int noOfApartments=10;
            System.out.println("Program to find maximum profit from given set of buyers and apartments. \n total number of apartments="+noOfApartments);
            int [] buyersNeed= {4, 1, 2, 3};
            int [] price={8, 1, 10, 9};
            print(buyersNeed);
            print(price);
            System.out.println("Maximum Profit="+findMaxProfit(noOfApartments,buyersNeed,price,price.length-1,0));
    
        }
    
        /**
        *b={4} and p={1} and noOfApratment=4 , max profit=4
        *b={4,1} and price{1,5} noOfApartment= 1, max profit=4
        *Start from right most and go forward for recurence
        **/
        public static int findMaxProfit(int noOfApartments, int need[], int[] price, int pos, int profit){
    
            if(need==null || price==null || need.length<1 || price.length<1 || price.length!=need.length|| pos<0 || pos>need.length) return profit;
            if(noOfApartments<=0) return profit;
            int currentProfit=0;
            if(need[pos]<=noOfApartments) currentProfit= profit+need[pos]*price[pos];
            System.out.println("No Of Apartments:"+noOfApartments+ "\t pos:"+pos+"\t profit:"+profit+ "\tneed[pos]"+need[pos]);noOfApartments
            System.out.println("No Of Apartments:"+noOfApartments+ "\t pos:"+pos+"\t profit:"+profit+ "\tneed[pos]"+need[pos]);noOfApartments
            return Math.max(findMaxProfit(noOfApartments-need[pos],need,price, pos-1,currentProfit), findMaxProfit(noOfApartments,need,price,pos-1,profit));
    
        }
    
    
        static void print(int... arr){
            if(arr==null || arr.length<1) return;
            System.out.print("[");
            for(int i:arr)System.out.print(i+" ");
            System.out.println("]");
        }
    }
    

    【讨论】:

      【解决方案3】:
      #include <stdio.h>
      #include <stddef.h>
      #include <stdlib.h>
      
      // Printing Matrix
      void printdpmatrix(int l1, int l2, int dp[l1+1][l2+1]){
          for (int i= 0; i < l1+1; i++){
              printf("\n");
              for (int j = 0 ; j < l2+1; j++){
                  printf("|%d|",dp[i][j]);
              }
          }
      }
      // Simple Max Function.
      int max(int a, int b){return a > b ? a : b;}
      
      // Returns maximum profit.
      int maxprofit(int forsale, int* demand, int* price, int length){
          int ans;
          int sell;
          int notsell;
          //Declare Dp Matrix, +1 to accomodate the 0/0 basecase.  
          int dp[length+1][forsale+1];
          //Initialise matrix
          for (int i = 0; i < forsale+1; i++){dp[0][i] = 0;}
          for (int j = 0; j < length+1; j++){dp[j][0] = 0;}
      
          // Main logic loop 
          // i is the buyers 
          // j is the apartments
          for (int i = 1; i < length +  1; i++){
              for (int j = 1; j < forsale + 1; j++){
                  //If we don't consider the current buyer we look at one buyer less. 
                  notsell = dp[i-1][j];
                  sell = 0; 
                  //If the demand exceeds the supply, we can't sell anything. 
                  if (demand[i-1] > j){ sell = 0;}
                  //If we have the supply to cope with the demaind then we take
                  //the profit of the current item and add the profit of this item 
                  //disconsidered and with the supply reduced by the demand of this item. 
                  else{ sell = price[i-1] + dp[i-1][j - (demand[i-1])];}
                  //Update the DP matrix.
                  dp[i][j] = max(sell,notsell);
                  }
          }
          // Print matrix for fun. 
          printdpmatrix(length, forsale, dp);
          // Answer = last position in matrix. 
          ans = dp[length][forsale];
          // Print the answer for fun. 
          printf(" \n %d <-ANS\n",ans);
          return ans;
      }
      int main(){
          int houses = 8;
          int demand[4] = {4,1,2,3};
          int price[4] = {8,1,10,9};
          int length = 4; 
          maxprofit(houses, demand, price, length);
      
          return 0;
      }
      

      迟到总比不到好;) 来自信息 2 的问候。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-06
        相关资源
        最近更新 更多