LoginSignup
1
0

More than 5 years have passed since last update.

単調増加数 (Ruby-alpha)

Last updated at Posted at 2018-05-30
問題 http://nabetani.sakura.ne.jp/hena/orde24tancho/
Ruby / AIR-lang / Python https://qiita.com/cielavenir/items/6de720c9a12813c5f469
Perl / C / Crystal / D / Go https://qiita.com/cielavenir/items/51f260519b62a3330234
Ruby-alpha https://qiita.com/cielavenir/items/9048c60498a9385b25c0

RubyKaigiの企業ブース(?)がネタ元です
http://tech.misoca.jp/entry/build-the-ruby-from-a-floppy-disk

ビルド方法

上記URLも参考に…

curl ftp://ftp.ruby-lang.org/pub/ruby/1.0/ruby-0.49.tar.gz -LO
tar xf ruby-0.49.tar.gz
cd ruby
curl https://raw.githubusercontent.com/charliesome/historical-rubies/master/ruby-0.49.patch -LO
patch -p1 < ruby-0.49.patch
./configure
make CFLAGS="-Wno-return-type -m32" # clang is ok.

解答

  • 繰り返しの構文が全く違う
  • 後置if不可
  • next不可
  • 3項演算子はif-then-else-endで代用
  • stdin/stdoutの明示が必要
  • putsは改行を付与しない
  • Bignumの除算が怪しいため、追加問題は解けません
tyama_henae24.rb0
#!/usr/bin/env ruby-alpha
#http://nabetani.sakura.ne.jp/hena/orde24tancho/
#https://qiita.com/Nabetani/items/928d6a94d83c21ef64d7

def comb(n,k)
    r=1
    for i in 0..k-1
        r=r*(n-i)/(i+1)
    end
    r
end

while $stdin.gets
    do a=$_.split(',').collect using e
        e.to_i-1
    end
    b=a[0]
    n=a[1]
    topdigit=1
    while !nil
        #b種類で作れるitopdigit桁の数
        x=comb(b,topdigit)
        if b<topdigit || x>n
            break
        end
        n-=x
        topdigit+=1
    end
    if b<topdigit
        $stdout.print("-\n")
    else
        topdigit-=1
        curnumber=0
        d=topdigit
        do topdigit.downto(0) using d
            curnumber+=1
            while !nil
                #b-curnumber種類の数で作れるd桁の数
                x=comb(b-curnumber,d)
                if x>n
                    break
                end
                n-=x
                curnumber+=1
            end
            $stdout.print((curnumber+(if curnumber < 10 then 48 else 87 end)).chr)
        end
        $stdout.print("\n")
    end
    $stdout.flush
end
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0