zoukankan      html  css  js  c++  java
  • 酒店价格

    题目描述

    酒店房间的价格录入是通过时间段来录入的,比如10月1日至10月7日800元,10月8日至10月20日500元,请实现以下函数int[][] merge(int[][] dateRangePrices),输入是某个酒店多个日期段的价格,每个日期段(终止日期大于等于起始日期)和对应的价格使用长度为3的数组来表示,比如[0, 19, 300], [10, 40, 250]分别表示从某天开始第1天到第20天价格都是300,第11天到第41天价格都是250,这些日期端有可能重复,重复的日期的价格以后面的为准, 请以以下规则合并并输出合并结果:
    1.相邻两天的价格如果相同,那么这两个日期段应该合并
    2.合并的结果应该以起始日期从小到大排序

    输入描述:

    输入数据包括多行,如样例输入所示。

    输出描述:

    输出数据为一行,如样例输出所示
    示例1

    输入

    1 1 100
    2 3 100
    4 5 110
    

    输出

    [1, 3, 100],[4, 5, 110]
     1 import java.util.ArrayList;
     2 import java.util.List;
     3 import java.util.Scanner;
     4 /**
     5  * 
     6  *  酒店价格
     7  *    把每天的价格 都放进一个数组里
     8  *    遍历
     9  * @author Dell
    10  *
    11  */
    12 public class Main {
    13 static public int[][] merge(int[][] datRangePrices){
    14     int maxDay = Integer.MIN_VALUE;
    15     int minDay = Integer.MAX_VALUE;
    16     //  天数范围
    17     for (int i = 0; i < datRangePrices.length; i++) {
    18     
    19             if (datRangePrices[i][1]>maxDay) {
    20                 maxDay=datRangePrices[i][1];
    21             }
    22             if (datRangePrices[i][0]<minDay) {
    23                 minDay = datRangePrices[i][0];
    24             }    
    25     }
    26     //  构造各天价格数组
    27     int [] dayPrice = new int[maxDay+1];//要求下标对应天数 需要加一
    28     // 初始化
    29     for (int i = 0; i < dayPrice.length; i++) {
    30         dayPrice[i]=0;
    31     }
    32     //  价格赋值
    33     for (int i = 0; i < datRangePrices.length; i++) {
    34         for (int j = datRangePrices[i][0]; j<=datRangePrices[i][1]; j++) {
    35             dayPrice[j]=datRangePrices[i][2];
    36         }
    37     }
    38     List<int[]> l = new ArrayList<>();
    39     // 装入 第一天  minDay
    40     int curPrice = dayPrice[minDay];
    41     int startDay = minDay;
    42     int endDay = minDay;
    43     // 从第二天循环
    44     for (int i = minDay+1; i < dayPrice.length; i++) {
    45         if (dayPrice[i]==0) {
    46             continue;
    47         }
    48         if (dayPrice[i]==curPrice) {
    49             endDay++;
    50         }else {
    51             l.add(new int[]{startDay,endDay,curPrice});
    52             startDay = i;
    53             endDay = i;
    54             curPrice = dayPrice[i];
    55         }
    56     }
    57     //  最后一个是跳出循环 需要处理
    58     endDay = maxDay;
    59         l.add(new int[]{startDay,endDay,curPrice});
    60     int res[][] = new int [l.size()][3];
    61     for (int i = 0; i < res.length; i++) {
    62         res[i] = l.get(i);
    63     }
    64     return res;
    65 } 
    66 public static void main(String[] args) {
    67 Scanner sc = new Scanner(System.in);
    68 List<int[]> list = new ArrayList();
    69 while (sc.hasNextInt()) {  //   按  Ctrl + z 结束输入
    70     int[] p = new int[3]; 
    71          p[0]=sc.nextInt();
    72          p[1]=sc.nextInt();
    73          p[2] = sc.nextInt();
    74          list.add(p);
    75     }
    76     int[][] price = new int[list.size()][3];
    77     price = list.toArray(price);//  将list里的数组转化进price toArray() 会得到object[]会导致转型失败 toArray(price) 里使用泛型可以转型
    78     int res[][] = merge(price);
    79 //    int res[][] = merge(new int[][] {{1,1,100},{2,3,100},{4,5,110}});
    80     String string = "";
    81     for (int i = 0; i < res.length; i++) {
    82         string+="["+res[i][0]+", "+res[i][1]+", "+res[i][2]+"],";
    83     }
    84     string = string.substring(0,string.length()-1);
    85     System.out.println(string);
    86 }
    87 }
  • 相关阅读:
    Dynamics AX
    专注于领域驱动设计的研究与实践系列转载
    在C#里使用属性,如Obsolete,Serializable,XmlRoot
    SQL 2005 with(nolock)详解
    Microsoft Domain Oriented NLayered .NET 4.0 App Sample (DDD Architecture)
    使用 .NET4 中的Task优化线程池【.NET4 多核并行】
    实现简单DTO适配器,解放你的双手
    最强悍的VS插件—reSharper
    通过代码配置 Log4net
    Microsoft NLayerApp案例理论与实践–DDD、分布式DDD及其分层【转】
  • 原文地址:https://www.cnblogs.com/the-wang/p/8981549.html
Copyright © 2011-2022 走看看