スポンサーサイト
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
新しい記事を書く事で広告が消せます。
/*
* ゴロム環の計算(Golomb.java) 第 3 版
*/
class Golomb {
final static int SIZE = 9;
final static int WORKSIZE = SIZE + SIZE;
static int aax[];
static int vals[];
static int maxnum;
static Ring ring;
public static void main( String args[] ) throws Exception {
aax = new int[ SIZE ];
vals = new int[ SIZE ];
maxnum = ( SIZE * (SIZE - 1 ) ) / 2 + 1;
vals[0]=1;
_combi( 1, 2, maxnum, SIZE - 1, SIZE * (SIZE - 1) );
System.out.print( "completed." );
System.exit( 0 );
}
/* 合計が maxnum になる組合せを作る */
static void _combi( int ord, int nx, int ny, int n, int sum ) {
if ( n == 1 ) {
if ( ( nx <= sum ) && ( sum <= ny ) ) {
vals[ SIZE - 1 ] = sum;
for ( int cnt = 0 ; cnt < SIZE ; cnt += 1 ) {
aax[ cnt ]= vals[ cnt ];
}
_golomb( 0 );
}
} else {
for ( int ns = nx ; ns < ( ny - n + 1 ) ; ns += 1 ) {
vals[ ord ] = ns;
_combi( ord + 1, ns + 1, ny, n - 1, sum - ns );
}
}
}
/* 組合せから順列を作る。*/
static void _golomb( int n ) {
if ( n < SIZE ) {
if ( n == 0 ) {
_golomb( 1 );
} else {
_golomb( n + 1 );
for (int cnt = n + 1 ; cnt < SIZE ; cnt +=1 ) {
// swap aax[ n ], aax[ cnt ]
int wk = aax[ n ];
aax[ n ] = aax[ cnt ];
aax[ cnt ] = wk;
_golomb( n + 1 );
// swap aax[ n ], aax[ cnt ]
wk = aax[ n ];
aax[ n ] = aax[ cnt ];
aax[ cnt ] = wk;
}
}
} else {
ring = new Ring( aax ); // このあたりが Java
if ( ring.check() ) {
ring.Print();
}
}
}
static class Ring {
static int MAXVAL;
int workx[];
Ring( int aa[] ) {
int cnt;
MAXVAL = ( SIZE - 1 ) * SIZE + 1;
this.workx = new int[ WORKSIZE ];
for ( cnt = 0 ; cnt < SIZE ; cnt += 1 ) {
this.workx[ cnt ] = aa[ cnt ];
this.workx[ cnt + SIZE ] = aa[ cnt ];
}
}
boolean check() {
int vtable[] = new int[ MAXVAL ]; // 0 要素を使わないかわりに、合計(MAXVAL自身)も出てこないから
for ( int cnt = 0 ; cnt < MAXVAL ; cnt += 1 ) {
vtable[ cnt ] = 0;
}
for ( int cnt = 0 ; cnt < SIZE ; cnt += 1 ) {
vtable[ this.workx[ cnt ] ] = 1;
}
for ( int posCnt = 0 ; posCnt < SIZE ; posCnt += 1 ) {
int sum = this.workx[ posCnt ];
for ( int lenCnt = 1 ; lenCnt < ( SIZE - 1 ) ; lenCnt += 1 ) {
sum += this.workx[ posCnt + lenCnt ];
if ( vtable[ sum ] == 0 ) {
vtable[ sum ] = 1;
} else {
return false;
}
}
}
return true;
}
/* 表示 */
void Print(){
for ( int cnt = 0 ; cnt < SIZE - 1 ; cnt += 1 ) {
System.out.print( this.workx[ cnt ] );
System.out.print( "-" );
}
System.out.println( workx[ SIZE - 1 ] );
}
}
}