黑色星期五队列管理


10

介绍

您是一家大型零售商店中电子部门的经理,一年中最大的销售日是本星期五。为了帮助管理人群,您的商店正在实施最大交易的票务系统,客户必须在购买商品之前出示票证。您的工作是编写一个程序来验证票证。

由于商店中唯一可用的计算机(由于预算削减)是一台损坏了键盘的恐龙(而且您所拥有的都是USB键盘,这些键盘不兼容),因此您必须用鼠标输入程序。因此,您的程序应尽可能短。

产品展示

您的商店正在使用以下五种不同的产品进行销售。每个产品都有一个全小写的名称,以及在一天中的什么时候可以购买多少个不同的规则。

  • television:有5库存的平板电视可能从00:00:00(午夜)到采购00:59:59
  • smartphone:有10智能手机库存,但从00:00:00(午夜)起排队的所有客户在00:59:59用完时会收到一张代金券。
  • tablet:有些10平板电脑可以随时购买。
  • laptop:从00:00:00(午夜)到可以购买数量不限的笔记本电脑07:59:59
  • lightbulb:随时可以购买无限数量的灯泡。

输入值

多行字符串,每行采用以下格式。行按时间戳排序。

<time stamp> <product name> <ticket number>
  • 票证号码为8位数字。最后一位数字是一个校验位,等于前七个数字模10的总和。要生效,票证号必须具有正确的校验码,并且必须严格大于所有先前的票证号。
  • 产品名称是上面列出的字符串之一。
  • 时间戳是一天中的格式的时间HH:MM:SS,其中HH是从00-23两位数小时,MM并且SS是两位数分钟和秒分别。

输出量

输出是以下字符串之一,每张票证一行。必须按顺序应用条件。

  1. Expired offer (适用于电视,智能手机和笔记本电脑。)门票的时间戳是在购买产品的截止日期之后。
  2. Invalid ticket 票证号小于或等于前一张票证的号,或者校验位无效。
  3. Give voucher (适用于智能手机。)该产品缺货,但是在报价过期之前排队的所有客户都可以进行雨淋检查。
  4. Out of stock(适用于电视和平板电脑。)所有产品均已售出。抱歉,数量有限。
  5. Accepted满足所有条件,所以给他们产品。请注意,只有接受的票证才能减少库存的物品数量。

Input                           Output
----------------------------    --------------
00:00:00 television 00010001    Accepted
00:00:25 smartphone 00011697    Accepted
00:01:25 laptop 00030238        Accepted
00:02:11 smartphone 00037291    Accepted
00:02:37 lightbulb 00073469     Invalid ticket
00:03:54 smartphone 00096319    Accepted
00:05:26 tablet 00152514        Accepted
00:06:21 tablet 00169893        Accepted
00:07:10 television 00190268    Accepted
00:07:47 smartphone 00194486    Accepted
00:07:55 tablet 00220071        Accepted
00:08:20 lightbulb 00321332     Accepted
00:10:01 smartphone 00409867    Accepted
00:11:10 tablet 00394210        Invalid ticket
00:11:46 television 00581060    Accepted
00:12:44 lightbulb 00606327     Accepted
00:13:16 tablet 00709253        Accepted
00:13:53 television 00801874    Accepted
00:14:47 laptop 00832058        Accepted
00:15:34 smartphone 00963682    Accepted
00:16:24 smartphone 01050275    Accepted
00:17:45 tablet 01117167        Accepted
00:18:05 laptop 01107548        Invalid ticket
00:19:00 lightbulb 01107605     Invalid ticket
00:19:47 lightbulb 01492983     Accepted
00:19:50 smartphone 01561609    Accepted
00:21:09 television 01567098    Accepted
00:21:42 laptop 01597046        Accepted
00:22:17 smartphone 01666313    Accepted
00:24:12 tablet 01924859        Accepted
00:24:12 smartphone 02151571    Accepted
00:25:38 smartphone 02428286    Give voucher
00:31:58 television 02435284    Out of stock
00:35:25 television 02435295    Out of stock
00:52:43 laptop 02657911        Invalid ticket
00:53:55 smartphone 02695990    Give voucher
01:08:19 tablet 02767103        Accepted
01:34:03 television 02834850    Expired offer
01:56:46 laptop 02896263        Accepted
02:02:41 smartphone 03028788    Expired offer
02:30:59 television 03142550    Expired offer
02:51:23 tablet 03428805        Accepted
03:14:57 smartphone 03602315    Expired offer
03:27:12 television 03739585    Expired offer
03:56:52 smartphone 03997615    Expired offer
04:07:52 tablet 04149301        Accepted
04:12:05 lightbulb 04300460     Invalid ticket
04:24:21 laptop 04389172        Accepted
04:40:23 lightbulb 04814175     Accepted
04:40:55 tablet 04817853        Accepted
04:42:18 smartphone 04927332    Expired offer
05:06:43 tablet 05079393        Out of stock
05:16:48 tablet 05513150        Out of stock
05:33:02 television 05760312    Expired offer
05:43:32 tablet 06037905        Out of stock
06:12:48 smartphone 06440172    Expired offer
06:35:25 laptop 06507277        Accepted
06:42:29 lightbulb 06586255     Invalid ticket
06:55:31 lightbulb 06905583     Accepted
06:55:33 lightbulb 06905583     Invalid ticket
07:40:05 smartphone 07428006    Expired offer
07:49:12 television 07588086    Expired offer
08:14:56 laptop 08111865        Expired offer

我试图使该示例涵盖所有可能的输出方案,但是如果不清楚,请发表评论。

这是,您可以编写程序或函数,并且不允许使用标准漏洞。

Answers:


5

使用Javascript(ES6),396个 433 419字节

h=0;f=1/0;i={television:{c:5,s:0,e:1},smartphone:{c:10,s:0,e:1,v:1},tablet:{c:10,s:0,e:24},laptop:{c:f,s:0,e:8},lightbulb:{c:f,s:0,e:24}};while(1){t=prompt().split(' ');a=t[0];r=a[0]+a[1]-0;o=i[t[1]];m=0;z='Accepted';u=t[2]-0;u<=h||(u-u%10+'').split('').reduce((p,c)=>-(-p-c))%10!=u%10?m='Invalid ticket':0;r<o.s||r>=o.e?m='Expired offer':0;if(!m)o.c?o.c--:(o.v?z='Give voucher':m='Out of stock');!m?h=u:0;alert(m?m:z)}

编辑:通过使用es6大箭头功能减小大小

更具可读性:

h=0
f=1/0
i={television:{c:5,s:0,e:1},smartphone:{c:10,s:0,e:1,v:1},tablet:{c:10,s:0,e:24},laptop:{c:f,s:0,e:8},lightbulb:{c:f,s:0,e:24}}
while(1){t=prompt().split(' ')
a=t[0]
r=a[0]+a[1]-0
o=i[t[1]]
m=0
z='Accepted'
u=t[2]
if(u<=h||(u-u%10+'').split('').reduce(function(p,c){return-(-p-c)})%10!=u%10)m='Invalid ticket'
if(r<o.s||r>=o.e)m='Expired offer'
if(!m){if(o.c)o.c--
else o.v?z='Give voucher':m='Out of stock'}if(!m)h=u
alert(m?m:z)}

有趣的是,较长的代码更快:http : //jsperf.com/compare-read

具有相同逻辑的GUI:

var app = angular.module('app', []);

app.controller('MainCtrl', function MainCtrl ($scope) {
  $scope.items = {
    television: {c: 5, s:0, e: 1},
    smartphone: {c: 10, s:0, e: 1},
    tablet: {c:10, s:0, e: 24},
    laptop: {c:Infinity, s: 0, e: 8},
    lightbulb: {c: Infinity, s: 0, e: 24},
  };
  
  var h = -1;//highest
  
  $scope.hours_offset = 0;
  
  $scope.ticket = {};
  for (var i = 0; i < 8; i++) $scope.ticket[i] = 0;
  
  $scope.selected_item = -1;
  
  $scope.nums = new Array(10);
  
  $scope.history = [];
  
  $scope.buy = function() {
    
  }
  $scope.selectItem = function($i) {
    $scope.selected_item = $i;
  }
  
  $scope.purchase = function() {
    if ($scope.selected_item === -1)
      return;

    var ticnum = '';
    var msg = 'Accepted';
    
    for(var key in $scope.ticket)
      ticnum+=$scope.ticket[key];
    
    var d = new Date();
    
    //Variable declarations to setup for code like the code I designed for console
    r = d.getHours()-$scope.hours_offset;//hour
    o = $scope.items[$scope.selected_item];//item
    m = 0//msg
    z = 'Accepted'//default_msg
    u=ticnum
    
    //This is copied from my console code
    if(ticnum<=h||(ticnum-ticnum%10+'').split('').reduce(function(p,c){return-(-p-c)})%10!=ticnum%10)m='Invalid ticket'
	if(r<o.s||r>=o.e)m='Expired offer'
	if(!m){if(o.c)o.c--
	else o.v?z='Give voucher':m='Out of stock'}if(!m)h=ticnum
	
    msg = m?m:z;
    
    $scope.history.unshift({
      time: time(d),
      item: $scope.selected_item,
      ticket_num: ticnum,
      message: msg
    });
    
  }
});

function time(date) {
  return padToTwo(date.getHours()) + ':' + padToTwo(date.getMinutes()) + ':' + padToTwo(date.getSeconds());
}
    
function padToTwo(number) {
  return number = ("00"+number).slice(-2);
}
#parent {
  width: 100vw;
  height: 100vh;
  
  padding-bottom: 25vh;
}
#parent > div {
  padding-top: 25vh;
  
  -webkit-display: flex;
  display: flex;
  
  -webkit-flex-direction: row;
  flex-direction: row;
  
  -webkit-align-items: flex-start;
  align-items: flex-start;
  -webkit-justify-content: center;
  justify-content: center;
}

#items {
  width: 15vw;
  min-width: 110px;
  margin-right: 4vw;
  background-color: #222223;
  color: #eeeeef;
  border-radius: 4px;
  border: 2px solid black;
}

#items > span {
  box-sizing: border-box;
  display: block;
  padding: 5px;
  
  -webkit-transition: .2s background-color ease-in-out;
  transition: .2s background-color ease-in-out;
  
  text-align: center;
  
  width: 100%;
  
  border-radius: 4px;
}
#items > span:hover, #ticket p:hover {
  background-color: gray;
}
#items > span.selected, #ticket p.selected {
  background-color: #999999;
}
#ticket {
  -webkit-display: flex;
  display: flex;
  
  -webkit-flex-direction: row;
  flex-direction: row;
  
  margin-right: 4vw;
  
  color: #eeeeef;
}

#ticket p {
  margin: 1px;
  padding: 3px;
  width: 20px;
  text-align: center;
  background-color: #22222f;
  border: 2px solid black;
  border-radius: 3px;
}

#purchase {
  padding: 15px;
  padding-top: 6px;
  padding-bottom: 6px;
  
  background-color: #22222f;
  border: 2px solid black;
  border-radius: 3px;
  color: #eeeeef;
}
#purchase:active {
  background-color: gray;
}

#error {
  margin-top: 15px;
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<div id="parent" ng-app="app" ng-controller="MainCtrl">
  For testing hours-offset (If it's 6:00 setting this to 18 would simulate it being midnight): <input ng-model="hours_offset" type="number"></input>
  <div>
    <div id="items">
      <span ng-attr-class="{{selected_item===name ? 'selected' : ''}}" ng-repeat="(name, obj) in ::items track by $index" ng-click="selectItem(name)">{{::name}}<br></span>
    </div>
    <div id="ticket">
      <span ng-repeat="(ticket_index, val) in ::ticket">
        <p ng-attr-class="{{ticket[ticket_index]===$index ? 'selected' : ''}}" ng-repeat="nothing in ::nums track by $index" ng-click="ticket[ticket_index] = $index">{{::$index}}</p>
      </span>
    </div>
    <span id="purchase" ng-click="purchase()">Purchase</span>
  </div>
  <div id="error">{{error_msg}}</div>
  <table id="output">
    <tr ng-repeat="item in history track by $index">
      <td ng-repeat="(key, value) in item track by $index">{{value}}</td>
    </tr>
  </span>
</div>


欢迎来到PPCG。虽然您的GUI是某种东西,但是代码高尔夫的目的是编写尽可能短的程序。由于您使用的是JavaScript,因此可以将输入字符串作为函数参数并返回输出。您还应该删除不必要的空格并缩短变量名。您为什么不看看其他一些问题来了解该网站的工作原理?
intrepidcoder

@intrepidcoder当然,您之后会缩小代码!但是基于这种情况,在我看来,如果没有键盘,控制台将无效!
csga5000

1
根据我们帮助中心中概述的规则,该帖子目前处于关闭状态。应对挑战的所有解决方案均应:[...]认真对待使用中的获胜标准。例如,参加代码高尔夫比赛的条目需要打高尔夫[。]
丹尼斯

对于短代码长度来说,这是荒谬的,与实际问题无关。这三个程序的总大小为4.51 KB。如果您不尝试缩小尺寸,则您的帖子可能会被删除。
intrepidcoder

@intrepidcoder哦,是的!我忘了,xD有很多约束。当您说最后一位数字时,您是指MSB还是LSB?我猜的是LSB,但我希望可以确定。
csga5000
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.