Skip to content

Instantly share code, notes, and snippets.

@tkihira
Created May 27, 2015 03:05
Show Gist options
  • Save tkihira/1483c90f26eb92021306 to your computer and use it in GitHub Desktop.
Save tkihira/1483c90f26eb92021306 to your computer and use it in GitHub Desktop.
1+23-4+56+7+8+9 = 100
#include <stdio.h>
void recursive(int level, int* op) {
if(level == 8) {
int result = 0;
int lastOp = 1;
int stack = 0;
for(int i = 0; i < 9; i++) {
int num = i + 1;
stack *= 10;
stack += num;
if(num <= 8) {
if(op[i] == 2) {
continue;
}
if(op[i] == 0) {
result += lastOp * stack;
lastOp = 1;
stack = 0;
}
if(op[i] == 1) {
result += lastOp * stack;
lastOp = -1;
stack = 0;
}
} else {
result += lastOp * stack;
}
}
if(result == 100) {
for(int i = 0; i < 8; i++) {
printf("%d", i + 1);
switch(op[i]) {
case 0: printf("+"); break;
case 1: printf("-"); break;
case 2: break;
}
}
printf("9=100\n");
}
return;
}
for(int i = 0; i < 3; i++) {
op[level] = i;
recursive(level + 1, op);
}
}
int main(void) {
int op[8] = {0,0,0,0,0,0,0,0};
recursive(0, op);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment