単調増加数 (Perl / C / Crystal / D / Go)


|問題|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|

GoはIIf(VB6と同様の、短絡しないなんちゃって3項)使ってます

Java/C\#等は今回は多分割愛します。。

## Perl

```pl:tyama_henae24.pl
#!/usr/bin/env perl
#http://nabetani.sakura.ne.jp/hena/orde24tancho/
#https://qiita.com/Nabetani/items/928d6a94d83c21ef64d7

use strict;
use warnings;
use IO::Handle;
STDOUT->autoflush(1);

sub comb{
	my($n,$k)=@_;
	my $r=1;
	for(my $i=0;$i<$k;$i++){
		$r=$r*($n-$i)/($i+1)
	}
	return $r
}

while(<>){
	my($b,$n)=split(',',$_);
	$b--;
	$n--;
	my $topdigit=1;
	for(;;){
		my $x=comb($b,$topdigit);
		last if($b<$topdigit || $x>$n);
		$n-=$x;
		$topdigit++
	}
	if($b<$topdigit){
		print "-\n";
		next
	}
	$topdigit--;
	my $curnumber=0;
	for(my $d=$topdigit;$d>=0;$d--){
		$curnumber++;
		for(;;){
			my $x=comb($b-$curnumber,$d);
			last if($x>$n);
			$n-=$x;
			$curnumber++
		}
		printf("%c",$curnumber+($curnumber<10 ? 48 : 87))
	}
	print("\n")
}
```

## C

```c:tyama_henae24.c
//http://nabetani.sakura.ne.jp/hena/orde24tancho/
//https://qiita.com/Nabetani/items/928d6a94d83c21ef64d7

#include <stdio.h>

long long comb(int n,int k){
	long long r=1;
	for(int i=0;i<k;i++){
		r=r*(n-i)/(i+1);
	}
	return r;
}
int main(){
	int b;
	long long n;
	for(;~scanf("%d,%lld",&b,&n);){
		b--;
		n--;
		int topdigit=1;
		for(;;){
			long long x=comb(b,topdigit);
			if(b<topdigit || x>n)break;
			n-=x;
			topdigit++;
		}
		if(b<topdigit){
			puts("-");
			fflush(stdout);
			continue;
		}
		topdigit--;
		int curnumber=0;
		for(int d=topdigit;d>=0;d--){
			curnumber++;
			for(;;){
				long long x=comb(b-curnumber,d);
				if(x>n)break;
				n-=x;
				curnumber++;
			}
			putchar(curnumber+(curnumber<10 ? 48 : 87));
		}
		puts("");
		fflush(stdout);
	}
}
```

## Crystal

```cr:tyama_henae24.cr
#!/usr/bin/env crystal
#http://nabetani.sakura.ne.jp/hena/orde24tancho/
#https://qiita.com/Nabetani/items/928d6a94d83c21ef64d7

def comb(n,k)
	r=1_i64
	k.times{|i|
		r=r*(n-i)/(i+1)
	}
	r
end

STDOUT.sync=true
while l_=gets
	line=l_.chomp.split(',')
	b=line[0].to_i-1
	n=line[1].to_i64-1
	topdigit=1
	loop{
		x=comb(b,topdigit)
		break if b<topdigit || x>n
		n-=x
		topdigit+=1
	}
	if b<topdigit
		puts :-
		next
	end
	topdigit-=1
	curnumber=0
	topdigit.downto(0){|d|
		curnumber+=1
		loop{
			x=comb(b-curnumber,d)
			break if x>n
			n-=x
			curnumber+=1
		}
		print curnumber.to_s(b+1)
	}
	puts
end
```

## D

```d:tyama_henae24.d
#!/usr/bin/env rdmd
//http://nabetani.sakura.ne.jp/hena/orde24tancho/
//https://qiita.com/Nabetani/items/928d6a94d83c21ef64d7

import core.stdc.stdio;

long comb(int n,int k){
	long r=1;
	for(int i=0;i<k;i++){
		r=r*(n-i)/(i+1);
	}
	return r;
}
int main(){
	int b;
	long n;
	for(;~scanf("%d,%lld",&b,&n);){
		b--;
		n--;
		int topdigit=1;
		for(;;){
			long x=comb(b,topdigit);
			if(b<topdigit || x>n)break;
			n-=x;
			topdigit++;
		}
		if(b<topdigit){
			puts("-");
			fflush(stdout);
			continue;
		}
		topdigit--;
		int curnumber=0;
		for(int d=topdigit;d>=0;d--){
			curnumber++;
			for(;;){
				long x=comb(b-curnumber,d);
				if(x>n)break;
				n-=x;
				curnumber++;
			}
			putchar(curnumber+(curnumber<10 ? 48 : 87));
		}
		puts("");
		fflush(stdout);
	}
	return 0;
}
```

## Go


```go:tyama_henae24.go
//usr/bin/env go run $0 $@;exit
//http://nabetani.sakura.ne.jp/hena/orde24tancho/
//https://qiita.com/Nabetani/items/928d6a94d83c21ef64d7

package main
import (
	"fmt"
	"os"
)

//acknowledgement: https://stackoverflow.com/questions/19979178/what-is-the-idiomatic-go-equivalent-of-cs-ternary-operator/#45886594
func IIf(statement bool, a, b interface{}) interface{}{
	if statement {
		return a
	}
	return b
}

func comb(n int,k int) int64{
	var r int64=1
	for i:=0;i<k;i++ {
		r=r*int64(n-i)/int64(i+1)
	}
	return r
}
func main(){
	b:=0
	var n int64
	for ;; {
		c,_:=fmt.Scanf("%d,%d",&b,&n)
		if c<2 {break}
		b-=1
		n-=1
		topdigit:=1
		for ;; {
			x:=comb(b,topdigit)
			if b<topdigit || x>n {break}
			n-=x
			topdigit++
		}
		if(b<topdigit){
			fmt.Print("-\n")
			os.Stdout.Sync()
			continue;
		}
		topdigit--
		curnumber:=0
		for d:=topdigit;d>=0;d-- {
			curnumber++
			for ;; {
				x:=comb(b-curnumber,d)
				if x>n {break}
				n-=x
				curnumber++
			}
			fmt.Printf("%c",curnumber+IIf(curnumber<10,48,87).(int));
		}
		fmt.Print("\n");
		os.Stdout.Sync()
	}
}
```