12 int find(int n
, int i1
, int a
, int b
, int op
)
18 stack_res
[3*stack_ptr
] = a
;
19 stack_op
[stack_ptr
] = op
;
20 stack_res
[3*stack_ptr
+1] = b
;
21 stack_res
[3*stack_ptr
+2] = n
;
27 for(i
=0;i
<nb_num
;i
++) {
28 for(j
=i
+1;j
<nb_num
;j
++) {
31 if (a
!= 0 && b
!= 0) {
36 if (find(a
+ b
, i
, a
, b
, '+'))
38 if (find(a
- b
, i
, a
, b
, '-'))
40 if (find(b
- a
, i
, b
, a
, '-'))
42 if (find(a
* b
, i
, a
, b
, '*'))
46 if (find(c
, i
, a
, b
, '/'))
52 if (find(c
, i
, b
, a
, '/'))
66 int main(int argc
, char **argv
)
71 printf("usage: %s: result numbers...\n"
72 "Try to find result from numbers with the 4 basic operations.\n", argv
[0]);
77 result
= atoi(argv
[p
]);
78 printf("result=%d\n", result
);
80 for(i
=p
+1;i
<argc
;i
++) {
81 tab
[nb_num
++] = atoi(argv
[i
]);
85 res
= find(0, 0, 0, 0, ' ');
87 for(i
=0;i
<=stack_ptr
;i
++) {
88 printf("%d %c %d = %d\n",
89 stack_res
[3*i
], stack_op
[i
],
90 stack_res
[3*i
+1], stack_res
[3*i
+2]);
94 printf("Impossible\n");