Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Train Carriage Game Solutions

Name: Anonymous 2012-08-26 2:11

I'm looking to program a function that can check if you can "make 10" from some 4 digit number (0000 to 9999).

The function should use these rules when performing the check:
https://www.facebook.com/pages/Train-Carriage-Game/108348062559174?sk=info

Does /prog/ know if there's a simple way to perform such a check short of checking every combination individually? I obviously don't want to be writing the following:


boolean makesTen (int a, int b, int c, int d) {
    if (a + b + c + d == 10) {
        return true;
    } else if (-a + b + c + d == 10) {
        return true;
    } else if (a - b + c + d == 10) {
        return true;
    }
    ...
    ...
    } else {
        return false;
    }
}

Name: Anonymous 2012-08-29 23:48

Am I being trolled? This is extremely easy.


#include <stdio.h>
#include <stdlib.h>

int main(void) {
  char buff[5];
  int n[4], i, num;
 
  int a, b, c, d;
 
  while(1) {
    fgets(buff, 5, stdin);
    buff[4] = '\0';
    if(!(num = atoi(buff))) {
      puts("NaN");
      continue;
    }
   
    num*=10;
    for(i = 0; i < 4; i++)
      n[i] = ((num/=10) % 10);
   
    for(a = -1; a <= 1; a += 2)
      for(b = -1; b <= 1; b += 2)
        for(c = -1; c <= 1; c += 2)
          for(d = -1; d <= 1; d += 2)
            if(a*n[0] + b*n[1] + c*n[2] + d*n[3] == 10) {
              puts("True");
              return(0);
            }
           
    puts("Nope");
  }
 
  return(1);
}


Also, to the person who suggested using recursion. Learn how to program.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List