首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >你能搞定酒吧吗?

你能搞定酒吧吗?
EN

Code Golf用户
提问于 2015-11-12 19:41:23
回答 8查看 1.1K关注 0票数 22

背景

星期五下午晚些时候,你和你的朋友决定那天晚上晚些时候去酒吧,但在去酒吧之前,你应该喝点饮料。然而,事态迅速升级;本周早些时候,你的朋友夏多克·潘普姆索斯中了彩票,并决定用不同的饮料把箱子放在板条箱上。酒馆的保安非常严格,如果你在试图进入酒店前过度消费,你是不允许进入的。不过,你们都是程序员--所以你觉得事情会变得很好。

挑战

你需要设计一个酒精计,如果你超过/低于合理的酒吧限制,它会输出真实/虚假的信息。在你下楼去酒吧之前,你要输入你晚上喝过的量和饮料类型到stdin,你的测量程序是这样写的。如果它输出的是真实的,你就会超出酒吧的限制,呆在家里。如果它输出的是假的,你就可以走了。

输入

一个比0大的整数,它代表你的体重(以公斤为单位),后面跟着一个换行符。这一输入后面是一系列一位数的数量和饮料,格式如下:

代码语言:javascript
复制
<amount><amount type>o<beverage type>

一瓶啤酒看起来是这样的:

代码语言:javascript
复制
1Bob

每个输入都由一个空格分隔。

输入规范

每种饮料都有一个对应于它所造成的影响的单位。如果你消耗的单位比你的体重除以二,酒吧不再是一种选择。

(这可能反映现实,也可能不反映现实)

以下为有效饮品及相应的酒类单位:

  • 啤酒:b1单元
  • 能量饮料:e0单位
  • 辣椒酱:h2 (烈性食品)
  • 果汁(由有机水果等制成):j0单位
  • 朗姆酒:r6单位
  • 龙舌兰酒:t7单位
  • 伏特加:v6单位
  • 葡萄酒:w3单位

有不同的数额类型:

  • 瓶:B
  • 箱子:C
  • 玻璃:G
  • 桶:K
  • Sip:S

每一种量都有一个乘数,将其中所含饮料的酒精单位乘以:

  • 瓶:3
  • 箱子:25
  • 玻璃:2
  • 桶:50
  • Sip:0.2

输出

如果消耗的量大于或低于体重除以2,则程序应将真/假输出到stdout。如果消耗的量等于体重除以2,则应输出错误。

可能输入和输出的

示例

输入

代码语言:javascript
复制
70
1Bob 3Soj

输出

代码语言:javascript
复制
False

输入

代码语言:javascript
复制
2
1Cov

输出

代码语言:javascript
复制
1

输入

代码语言:javascript
复制
50
1Cob

输出

代码语言:javascript
复制
0

输入

代码语言:javascript
复制
100
4Gow 1Koe 1Bov 1Gow 2Sot

输出

代码语言:javascript
复制
True

以字节为单位的最短程序获胜!

EN

回答 8

Code Golf用户

回答已采纳

发布于 2015-11-15 05:49:07

CJam,53字节

代码语言:javascript
复制
6:B50:C2*:K4:G.4:S];q"behjrtvwo ""10206763*"er~*]:-U<

CJam解释器网上试一试。

是如何工作的

代码语言:javascript
复制
6:B          e# Push 6 and save it in B.
50:C         e# Push 50 and save it in C.
2*:K         e# Multiply by 2 to push 100 and save it in K.
4:G          e# Push 4 and save it in G.
.4:S         e# Push 0.4 and save it in S.
             e#
             e# The letters representing the types will now push its doubled
             e# (to avoid diving the weight by 2) associated multiplier.
];           e# Clear the stack.
q            e# Read all input.
"behjrtvwo " e# Push the string of beverages, concatenated with "o ".
"10206763*"  e# Push the string of associated units of alcohol and '*'.
er           e# Transliterate. This replaces each beverage letter with the
             e# associated units of alcohol, and each 'o' and ' ' with '*'.
             e#
             e# For example, the input
             e# 70
             e# 1Bob 3Soj
             e# is transformed into
             e# 70
             e# 1B*1*3S*0
             e#
~            e# Evaluate the resulting string.
             e#
             e# For the example this does the following:
             e#   + Push 70.
             e#   + Push 1, push 6, multiply, push 1, multiply.
             e#   + Push 3, push 0.4, multiply, push 0.
             e#
*            e# Multiply the last two (for the lack of a trailing space).
]            e# Collect all results in an array.
:-           e# Reduce by subtraction; subtract all other elements from the
             e# first element (body weight).
U<           e# Compare the result with 0.
票数 5
EN

Code Golf用户

发布于 2015-11-12 21:26:19

Python 3,131

现在我们在用蛇打高尔夫球!

保存了18个字节,这要归功于shebang。

由于DSM,节省了4个字节。

多亏了tzaman,节省了很多字节。

这要感谢扎曼滥用.find()的绝妙技巧,如果它找不到值的话,就会返回-1

目前,这假设这种饮料格式正是它在挑战中所陈述的方式,例如,每种饮料只有1位数的价值。

代码语言:javascript
复制
w=input()
print(sum([6,50,4,100,.4]['BCGKS'.find(b)]*int(a)*int('1267730'['bhrtvw'.find(v)])for a,b,_,v in input().split())>int(w))
票数 9
EN

Code Golf用户

发布于 2015-11-15 05:28:22

CJam,54字节

代码语言:javascript
复制
ldlS/{A,s"CbretjvwSBK"+f#A,[25X6T7T6Z.2Z50Y]+f=:*-}/0<

有点微妙,而且可能不太理想,但我认为这样做还行。在网上试试

解释

代码语言:javascript
复制
ld             Read first line, convert to double
lS/            Read second line, split by space
{...}/         For each item in the second line...
  A,s"..."+f#    Get index in "0123456789CbretjvwSBK", or -1 if not found
  A,[...]+f=     Index into [0 1 2 3 4 5 6 7 8 9 25 1 6 0 7 0 6 3 0.2 3 50 2]
  :*             Take product
  -              Subtract from weight
0<             Check if < 0

注意,数字数组的末尾有2,这意味着第一个字符串中缺少的Gho被映射到2。

票数 4
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/63686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档