Moderator: Board moderators
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMBERS 100
#define FALSE 0
#define TRUE 1
/* Function Prototypes */
void backtrack( char a[], int k, int numbers[], int num_numbers, int target, int sum );
int is_a_solution( int k, int num_numbers );
void process_solution( char a[], int k, int numbers[], int num_numbers, int target, int sum );
void construct_candidates( char a[], int k, int numbers[], char c[], int *ncandidates, int result );
/* Globals */
int finished; /* found all solutions yet? */
main() {
int num_cases, num_numbers, target, i, j;
int numbers[MAX_NUMBERS];
char a[MAX_NUMBERS]; /* Soln vector to send to backtrack */
/* Get number of cases */
scanf("%d\n", &num_cases);
/* Loop through all cases */
for( i = 0; i < num_cases; i++ ) {
/* Get num of numbers */
scanf("%d ", &num_numbers);
/* Get all numbers */
for( j = 0; j < num_numbers; j++ ) {
scanf("%d ", &numbers[j]);
}
/* Get target number */
scanf("%d", &target);
/* Call backtrack algorithm on these numbers */
finished = FALSE;
backtrack( a, 0, numbers, num_numbers, target, numbers[0] );
if( finished == FALSE )
printf("NO EXPRESSION");
printf("\n");
}
}
/* Backtrack algo */
void backtrack( char a[], int k, int numbers[], int num_numbers, int target, int sum ) {
char c[4]; /* candidates for next position */
int ncandidates; /* next position candidate count */
int i; /* counter */
int sum2;
if( is_a_solution( k, num_numbers ) == TRUE )
process_solution( a, k, numbers, num_numbers, target, sum );
else {
k = k + 1;
construct_candidates( a, k, numbers, c, &ncandidates, sum );
for ( i = 0; i < ncandidates; i++ ) {
a[k] = c[i];
switch( c[i] ) {
case '+': sum2 = sum + numbers[k]; break;
case '-': sum2 = sum - numbers[k]; break;
case '*': sum2 = sum * numbers[k]; break;
case '/': sum2 = sum / numbers[k]; break;
}
backtrack( a, k, numbers, num_numbers, target, sum2 );
if (finished == TRUE) return; /* terminate early */
}
}
}
int is_a_solution( int k, int num_numbers ) {
if( k == num_numbers - 1 ) {
return TRUE;
}
return FALSE;
}
void process_solution( char a[], int k, int numbers[], int num_numbers, int target, int sum ) {
int i;
if( sum == target ) {
for( i = 0; i < num_numbers - 1; i++ ) {
printf("%d%c", numbers[i], a[i+1]);
}
printf("%d=%d", numbers[num_numbers-1], target);
finished = TRUE;
}
}
void construct_candidates( char a[], int k, int numbers[], char c[], int *ncandidates, int result ) {
*ncandidates = 0;
/* Check '/' */
if( result % numbers[k] == 0 ) {
c[*ncandidates] = '/';
*ncandidates = *ncandidates + 1;
}
/* Check '*' */
if( result * numbers[k] <= 32000 && result * numbers[k] >= -32000 ) {
c[*ncandidates] = '*';
*ncandidates = *ncandidates + 1;
}
/* Check '-' */
if( result - numbers[k] >= -32000 ) {
c[*ncandidates] = '-';
*ncandidates = *ncandidates + 1;
}
/* Check '+' */
if( result + numbers[k] <= 32000 ) {
c[*ncandidates] = '+';
*ncandidates = *ncandidates + 1;
}
}
#9#
3 5 7 4 3#
2 1 1 2000#
5 12 2 5 1 2 4#
2 32000 2 16000#
2 32000 32000 1#
9 9 1 2 -10 33 3 3 2 2 23#
50 9 1 2 -10 33 3 3 2 2 23 9 1 2 -10 33 3 3 2 2 23 9 1 2 -10 33 3 3 2 2 23 9 1 2 -10 33 3 3 2 2 23 9 1 2 -10 33 3 3 2 2 23 109#
2 31998 2 32000#
3 -31997 -2 1 -32000#5+7/4=3#
NO EXPRESSION#
12+2-5-1/2=4#
32000/2=16000#
32000/32000=1#
9+1+2+-10*33+3/3+2-2=23#
9+1+2+-10+33+3+3+2+2+23+9+1+2+-10+33+3+3+2+2+23+9+1+2+-10+33+3+3+2+2+23+9+1+2+-10+33+3+3+2+2+23+9+1+2--10-33-3/3+2-2+23=109#
31998+2=32000#
-31997+-2-1=-32000#+ - * / #include <stdio.h>
char sign[] = "+-*/", stack[110];
int top, x[110], t, k, found, n;
int expression(int a, int b, char c)
{
int z;
if(c == '+')
z = a+b;
else if(c == '-')
z = a-b;
else if(c == '*')
z = a*b;
else
{
if(!b || a%b)
return -99999;
z = a/b;
}
if(z > 32000 || z < -32000)
return -99999;
return z;
}
void printStack()
{
int i;
for(i = 0; i < top; ++i)
printf("%d%c", x[i], stack[i]);
printf("%d=%d\n", x[i], t);
}
void solve(int p, int sum)
{
int i, z, val;
if(found)
return;
val = x[k];
z = expression(sum, val, sign[p]);
if(z == -99999)
return;
stack[top++] = sign[p];
if(top == n-1 && z != t)
{
--top;
return;
}
if(top == n-1 && z == t)
{
printStack();
found = 1;
return;
}
k++;
for(i = 0; sign[i]; ++i)
if(!found)
solve(i, z);
--k;
--top;
}
void main()
{
int test, tc, i;
scanf("%d", &test);
for(tc = 0; tc < test; ++tc)
{
scanf("%d", &n);
for(i = 0; i < n; ++i)
scanf("%d", &x[i]);
scanf("%d", &t);
if(n == 1)
{
if(x[0] == t)
printf("%d=%d\n", t);
else
printf("NO EXPRESSION\n");
continue;
}
k = 1;
found = top = 0;
for(i = 0; sign[i]; ++i)
if(!found)
solve(i, x[0]);
if(found == 0)
printf("NO EXPRESSION\n");
}
}ACed...
sumantbhardvaj wrote:I am aquainted with Bit masking concept.. but here in this situation, i could not imagine how bits can be used to mark the reached position ( as mentioned by Adrian )
if we do it by an array we need a 64000 x 100 size of array.. but masking can be done only for integers upto 64.
Can anybody explain this concept to me ? so that i could improve upon my runtime in this problem.
Users browsing this forum: No registered users and 0 guests