動作環境
C++ Builder XE4
関連 http://qiita.com/7of9/items/37d5287d0f6c7f4e0d7b
4色の色合成をC++ BuidlerのTCanvasで行う。
その前段階として、RGB値を得るためのインデックスのリストを取得する。
http://stackoverflow.com/questions/12991758/creating-all-possible-k-combinations-of-n-items-in-c
に記載のdorserg氏のコードをベースにC++ Builder実装してみた。
static宣言が残る点は要改善点。
クラス化なども検討した方がいい。
Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE で管理されるコンポーネント
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // ユーザー宣言
// { combination関連
int m_comb_pos;
std::vector<int> m_comb_people;
std::vector<int> m_comb_combination;
void __fastcall comb_setResult(const std::vector<int>& res);
void __fastcall comb_go(int offset, int left);
// } combination関連
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <iostream>
#include <vector>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
/*
original at
http://stackoverflow.com/questions/12991758/creating-all-possible-k-combinations-of-n-items-in-c
then modified
*/
static const int kNumMember = 4;
static const int kMaxList = 50;
static int s_colorList[kMaxList][kNumMember] = { 0 }; // 4 colors
void __fastcall TForm1::comb_setResult(const std::vector<int>& res)
{
String msg;
for(int idx=0; idx < res.size(); idx++) {
s_colorList[m_comb_pos][res[idx]] = true;
}
m_comb_pos++;
}
void __fastcall TForm1::comb_go(int offset, int left)
{
if (left == 0) {
comb_setResult(m_comb_combination);
return;
}
for (int idx = offset; idx <= m_comb_people.size() - left; idx++) {
m_comb_combination.push_back(m_comb_people[idx]);
comb_go(idx+1, left-1);
m_comb_combination.pop_back();
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int idx=0; idx < kNumMember ; idx++) {
m_comb_people.push_back(idx); // starting from [0]
}
m_comb_pos = 0;
comb_go(0, 1); // k==1
comb_go(0, 2); // k==2
comb_go(0, 3); // k==3
comb_go(0, 4); // k==4
for(int lidx=0; lidx < m_comb_pos; lidx++) { // lidx: list index
String msg = IntToStr(lidx) + L":";
for(int midx=0; midx < kNumMember; midx++) { // midx: member index
msg = msg + L" " + IntToStr(s_colorList[lidx][midx]);
}
OutputDebugString(msg.c_str());
}
}
//---------------------------------------------------------------------------
結果
デバッグ出力: 0: 1 0 0 0 プロセス Project1.exe (3896)
デバッグ出力: 1: 0 1 0 0 プロセス Project1.exe (3896)
デバッグ出力: 2: 0 0 1 0 プロセス Project1.exe (3896)
デバッグ出力: 3: 0 0 0 1 プロセス Project1.exe (3896)
デバッグ出力: 4: 1 1 0 0 プロセス Project1.exe (3896)
デバッグ出力: 5: 1 0 1 0 プロセス Project1.exe (3896)
デバッグ出力: 6: 1 0 0 1 プロセス Project1.exe (3896)
デバッグ出力: 7: 0 1 1 0 プロセス Project1.exe (3896)
デバッグ出力: 8: 0 1 0 1 プロセス Project1.exe (3896)
デバッグ出力: 9: 0 0 1 1 プロセス Project1.exe (3896)
デバッグ出力: 10: 1 1 1 0 プロセス Project1.exe (3896)
デバッグ出力: 11: 1 1 0 1 プロセス Project1.exe (3896)
デバッグ出力: 12: 1 0 1 1 プロセス Project1.exe (3896)
デバッグ出力: 13: 0 1 1 1 プロセス Project1.exe (3896)
デバッグ出力: 14: 1 1 1 1 プロセス Project1.exe (3896)