0
0

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.

網羅パターン生成 JScript

0
Posted at

ちょくちょく条件網羅パターンを書くことがあるので、自動生成用に作成。

作ったやつ

コード

// - - - - - - - - - - - - - - - - - - - - -
// ユーザ設定

var pattern = ["0", "1"];
var height = 4;

// - - - - - - - - - - - - - - - - - - - - -
// 網羅パターン生成

var width = Math.pow(pattern.length, height);

var matrix = [];
var blockSize = pattern.length;
while (blockSize <= width) {
  var row = [];
  
  for (var i = 1; i <= blockSize; i++) {
    for (var j = 1; j <= (width / blockSize); j++) {
      row.push(pattern[i % pattern.length]);
    }
  }
  
  matrix.push(row.join("\t"));
  blockSize += blockSize;
}

// - - - - - - - - - - - - - - - - - - - - -
// ファイル出力

var fs = WScript.CreateObject("Scripting.FileSystemObject");
var file = fs.CreateTextFile("output.txt");
file.write(matrix.join("\n"));
file.close();

出力ファイル

上記のコードから出力したファイルの内容。

1	1	1	1	1	1	1	1	0	0	0	0	0	0	0	0
1	1	1	1	0	0	0	0	1	1	1	1	0	0	0	0
1	1	0	0	1	1	0	0	1	1	0	0	1	1	0	0
1	0	1	0	1	0	1	0	1	0	1	0	1	0	1	0

これを Excel に直接貼り付けたり、行列を入れ替えたりして使う。変数 pattern を書き換えることで別の文字を使って出力することもできる。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?