■
次のコード、上も下もOKなのに
// OK long tmp[2] = {10L, 20L}; plus<long> add; cout << add(tmp[0], tmp[1]) << endl; --- // OK long tmp[1][2] = {{10L, 20L}}; plus<long> add; cout << add(tmp[0][0], tmp[0][1]) << endl;
なんで下のコードはエラーになるのかなー。
CountExpressions.cpp:48: error: no match for call to ‘(std::binary_function
とか言われるよ。どうしてアンパサンドがついてしまっているのだろうか。
int calcExpressions(int x_, int y_, int val) { long x = long(x_), y = long(y_); long xs[6][4] = { {x, x, y, y}, {x, y, x, y}, {x, y, y, x}, {y, y, x, x}, {y, x, y, x}, {y, x, x, y}}; plus<long> add; minus<long> sub; multiplies<long> mul; binary_function<long, long, long> opf[3] = {add, sub, mul}; char opc[3] = {'+', '-', '*'}; int count = 0; for(int i=0; i < 6; i++){ for(int op1=0; op1 < 3; op1++){ long v = opf[op1](xs[i][0], xs[i][1]); for(int op2=0; op2 < 3; op2++){ v = opf[op2](v, xs[i][2]); for(int op3=0; op3 < 3; op3++){ v = opf[op3](v, xs[i][3]); if(v == val){ cout << xs[i][0] << opc[op1] \ << xs[i][1] << opc[op2] \ << xs[i][2] << opc[op3] \ << xs[i][3] << endl; count++; } } } } } return 0; }
問題文はアカウントがあれば下のURLでみられるようだ。
http://www.topcoder.com/stat?c=problem_statement&pm=8157&rd=12170
-
-
-
- -
-
-
仕方がないのでif文で計算内容を変える関数を別に作ってあとvの使い回しに起因するバグを取ってsubmitした。system testに通った。だいぶやり方はわかってきた。木曜日が楽しみだ。