数字の間に数学演算(この場合は+/-)を挿入するか、要求された数値を取得するためにそれらをマージする必要がある問題を解決しようとしています。
例:123456789 => 123+4-5+6-7+8-9 = 120
私のコンセプトは、基本的に、配列内の演算コードのさまざまな組み合わせを生成し、ある数値と等しくなるまで式を計算することです。
問題は、再帰を使用して数学演算の可能なすべての組み合わせを生成する方法を考えることができないことです。
コードは次のとおりです。
#include <iostream>
#include <algorithm>
using namespace std;
enum {noop,opplus,opminus};//opcodes: 0,1,2
int applyOp(int opcode,int x, int y);
int calculate(int *digits,int *opcodes, int length);
void nextCombination();
int main()
{
int digits[9] = {1,2,3,4,5,6,7,8,9};
int wantedNumber = 100;
int length = sizeof(digits)/sizeof(digits[0]);
int opcodes[length-1];//math symbols
fill_n(opcodes,length-1,0);//init
while(calculate(digits,opcodes,length) != wantedNumber)
{
//recursive combination function here
}
return 0;
}
int applyOp(int opcode,int x, int y)
{
int result = x;
switch(opcode)
{
case noop://merge 2 digits together
result = x*10 + y;
break;
case opminus:
result -= y;
break;
case opplus:
default:
result += y;
break;
}
return result;
}
int calculate(int *digits,int *opcodes, int length)
{
int result = digits[0];
for(int i = 0;i < length-1; ++i)//elem count
{
result = applyOp(opcodes[i],result,digits[i+1]);//left to right, no priority
}
return result;
}