Last active
June 26, 2016 09:40
-
-
Save hiterm/8e34e8615d7f1e5f16004d0a41812c80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* maxは、どの文字がいくつあるかという配列 | |
その文字で出来る語の総数を求める関数 */ | |
long int gem_total(int max[], int n_max){ | |
int i, check = 0; | |
long int total; | |
for(i=0; i<n_max; i++){ | |
if(max[i] != 0){ | |
check = 1; | |
break; | |
} | |
} | |
if(check == 0){ | |
return 0; | |
} | |
/* 再帰で */ | |
total = 0; | |
for(i=0; i<n_max; i++){ | |
if(max[i] != 0){ | |
total++; | |
max[i]--; | |
total += gem_total(max, n_max); | |
max[i]++; | |
} | |
} | |
return total; | |
} | |
/* 全ての語の中から、配列objectが表す語が辞書順で何番目かを求める関数 | |
objectは、abcなら{0, 1, 2}みたいな感じの配列*/ | |
long int gem_find(int max[], int n_max, int object[], int l){ | |
int i, j; | |
long int total = 0; | |
/* gem_totalを使って求める */ | |
for(i=0; i<l; i++){ | |
if(i > 0) | |
max[object[i-1]]--; | |
for(j=0; j<object[i]; j++){ | |
if(max[j] > 0){ | |
max[j]--; | |
total++; | |
total += gem_total(max, n_max); | |
max[j]++; | |
} | |
} | |
total++; | |
} | |
return total; | |
} | |
/* こっちは宝石6個の場合 */ | |
/* int main(void){ */ | |
/* int max[3] = {3, 1, 2}; */ | |
/* int object[3] = {2, 2, 1}; */ | |
/* printf("%d\n" ,gem_find(max, 3, object, 3)); */ | |
/* } */ | |
int main(void){ | |
int i; | |
/* aが1つ、bが4つ、みたいな感じ */ | |
int max[7] = {1, 4, 1, 4, 2, 1, 3}; | |
/* eagcdfbeのこと */ | |
int object[8] = {4, 0, 6, 2, 3, 5, 1, 4}; | |
for(i=0; i<8; i++){ | |
printf("%c", object[i]+'a'); | |
} | |
printf("\n"); | |
printf("%ld\n", gem_find(max, 7, object, 8)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
def gemstring(max) | |
n_max = max.length | |
if max.select{|e| e == 0}.length == n_max | |
return 0 | |
end | |
total = 0 | |
n_max.times do |i| | |
if max[i] != 0 | |
max[i] -= 1 | |
total += 1 | |
total += gemstring(max) | |
max[i] += 1 | |
end | |
end | |
return total | |
end | |
def gem_find(max, object) | |
l = object.length | |
total = 0 | |
l.times do |i| | |
object[i].times do | |
end | |
end | |
end | |
max = [3, 1, 2] | |
p gemstring(max) | |
max = [1, 4, 1, 4, 2, 1, 3] | |
p gemstring(max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment