首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >给我的微波炉编程

给我的微波炉编程
EN

Code Golf用户
提问于 2012-10-24 00:24:50
回答 2查看 5.4K关注 0票数 12

我很懒,所以我总是用最少的按钮来编程我的微波炉。我的微波炉有以下按钮:

  • 一个“分钟加”按钮,只能先按下,意味着“开始”。它可能被多次按下多分钟,但它不会给手动输入的时间增加一分钟。输出+
  • 一组0-9的按钮。时间输入为MMSS (即"130“表示1分30秒)。秒可能在0.99之间。因此,"130“和"90”是等同的条目。显然,即使条目的秒部分超过59秒,每分钟也是60秒。输出0**.**9
  • 如果手动输入时间,则必须按下启动微波的“开始”按钮。输出S

我的食品包装指定了MM:SS的时间,所以程序必须接受这个输入。

示例

  • 1:00是+ (记住“分钟加”意味着开始)
  • 1:01是61S (秒数可以超过59秒,但是“分钟+”不能与数字一起工作-我认为这是我的微波炉中的一个设计缺陷)
  • 9:00是900S (比+++++++++短)
EN

回答 2

Code Golf用户

发布于 2012-10-24 01:41:26

JavaScript

代码语言:javascript
复制
var x = /(\d+):(\d\d)/.exec('<time here>');
x[1] === '0' ? +x[2] + 'S' :
x[1] < 4 && x[2] === '00' ? (
    x[1] === '1' ? '+' :
    x[1] === '2' ? '++' : '+++') :
x[2] < 40 ?
    (x[1] - 1 ? x[1] - 1 : '') + '' + (6 + +x[2][0]) + x[2][1] + 'S' :
x[1] + x[2] + 'S'
票数 4
EN

Code Golf用户

发布于 2012-10-24 21:11:34

ruby

代码语言:javascript
复制
#Build a string for the microwave
def build_result(minutes, seconds)
  duration = minutes * 60 + seconds  
  if duration < 99
    result = "%iS" % [ duration]    #shortcut '90S' instead '130S'
  else
    result = "%i%02iS" % [ minutes, seconds]
  end  
  result
end

#Call microwave optimizer
def microwave( input )
  minutes  = input.split(/:/).first.to_i 
  seconds = input.split(/:/).last.to_i

  #build result
  result = build_result(minutes, seconds)
  #try a shorter result, make 999S out of '10:39':
  if seconds < 40 and minutes > 0
    result2 = build_result(minutes - 1, seconds + 60)   #try a 
    result = ( result.size <= result2.size ? result : result2 )
  end

  #Check if a version with only '+' is shorter
  if seconds == 0 and minutes <= result.size
    result = '+' * minutes
  end
  result
end

#Test if called with an argument
if ARGV.empty?
  require 'test/unit'   #Exceute a test
  class MicrowaveTest < Test::Unit::TestCase
    def test_007
      assert_equal('7S', microwave('0:07'))
    end  
    def test_100
      assert_equal('+', microwave('1:00'))
    end
    def test_101
      assert_equal('61S', microwave('1:01'))
    end  
    def test_130
      assert_equal('90S', microwave('1:30'))
    end  
    def test_400
      #~ assert_equal('400S', microwave('4:00'))
      assert_equal('++++', microwave('4:00'))
    end  
    def test_500
      assert_equal('500S', microwave('5:00'))
    end  
    def test_900
      assert_equal('900S', microwave('9:00'))
    end 
    def test_1000
      #~ assert_equal('1000S', microwave('10:00'))
      assert_equal('960S', microwave('10:00'))
    end 
    def test_1015
      #~ assert_equal('1015S', microwave('10:15'))
      assert_equal('975S', microwave('10:15'))
    end 
    def test_1039
      #~ assert_equal('1039S', microwave('10:39'))
      assert_equal('999S', microwave('10:39'))
    end 
  end
else  #started via shell, evaluate input
  puts microwave(ARGV.first)
end

备注:

  • 使用ruby program-my-microwave-oven.rb启动它,并对单元测试进行评估。
  • ruby program-my-microwave-oven.rb 10:00启动它,然后编写960S

关于规则的几点意见(以及我的解释):

  • 10:00的最短时间是960S (9分60秒-> 10分钟)。
  • 10:39的最短时间是999S (9分99秒-> 10分39秒)。
  • 对于4:00来说,它更喜欢++++ (少动手指)
票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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