掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
組み合わせ。前回の続きです。ごめんなさい。 (ID:16346)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
「C言語による最新アルゴリズム事典」から移植。 とりあえずビール…じゃなくて…17_C_3さんのに比べると酷く技巧的で何が何やらわかりません。 x and -x っていうのはnクイーン問題とかでも使われていたテクニック(1が立っている右端のbitを得る)っていうのは分かるんですが・・・ Const N =17; Const K =3; (*********************************************************** 組合せの生成 ***********************************************************) function first( n :Longword) :Longword; begin first := ((1 shl n) - 1); end; function nextset(x :Longword ):Longword; var smallest, ripple, new_smallest, ones :Longword ; begin smallest := x and -x; ripple := x + smallest; new_smallest := ripple and -ripple; ones := ((new_smallest div smallest) shr 1) - 1; Result := (ripple or ones) ; end; procedure printset(s : Longword ); var i :Integer; St:AnsiString; begin St := ''; i := 1; while( i <= N) do begin if ((s and 1) <> 0 )then St := St + Format(' %d', [i]) ; s := s shr 1; Inc(i); end; Form1.Memo1.Lines.Add(St); end; procedure TForm1.Button1Click(Sender: TObject); var i :Integer; x,dbg : Longword ; begin i := 1; x := first(K); dbg := (not first(N)); while( (x and dbg ) = 0 )do begin Form1.Memo1.Lines.Add(Format(' %4d:', [i])); printset(x); x := nextset(x); Inc(i); dbg := (not first(N)) ; end; end;
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.