13 int find(int n
, int i1
, int a
, int b
, int op
)
19 stack_res
[3*stack_ptr
] = a
;
20 stack_op
[stack_ptr
] = op
;
21 stack_res
[3*stack_ptr
+1] = b
;
22 stack_res
[3*stack_ptr
+2] = n
;
28 for(i
=0;i
<nb_num
;i
++) {
29 for(j
=i
+1;j
<nb_num
;j
++) {
32 if (a
!= 0 && b
!= 0) {
37 if (find(a
+ b
, i
, a
, b
, '+'))
39 if (find(a
- b
, i
, a
, b
, '-'))
41 if (find(b
- a
, i
, b
, a
, '-'))
43 if (find(a
* b
, i
, a
, b
, '*'))
47 if (find(c
, i
, a
, b
, '/'))
53 if (find(c
, i
, b
, a
, '/'))
67 int main(int argc
, char **argv
)
72 printf("usage: %s: result numbers...\n"
73 "Try to find result from numbers with the 4 basic operations.\n", argv
[0]);
78 result
= atoi(argv
[p
]);
79 printf("result=%d\n", result
);
81 for(i
=p
+1;i
<argc
;i
++) {
82 tab
[nb_num
++] = atoi(argv
[i
]);
86 res
= find(0, 0, 0, 0, ' ');
88 for(i
=0;i
<=stack_ptr
;i
++) {
89 printf("%d %c %d = %d\n",
90 stack_res
[3*i
], stack_op
[i
],
91 stack_res
[3*i
+1], stack_res
[3*i
+2]);
95 printf("Impossible\n");