1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-05-26
問題 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

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

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

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

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

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()
	}
}
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?