From: tan@casbah.acns.nwu.edu (Shufeng Tan)
Subject: A C program for 24 points poker game
Organization: Northwestern University, Evanston Illinois.
Date: Thu, 17 Dec 1992 23:53:20 GMT

/* 24-point poker game */

/* Written by Shufeng Tan, who will not be responsible for any possible
   damaged to your computer caused by running the program.  Please send
   comments, if you have any, to tan@sunny.chem-eng.nwu.edu */

/* This program finds a solution to the 24-point problem if such a
   solution exists.  Save this article to a file, say 24.c, delete the new
   header, then compile the file: cc 24.c.  The program should run fine
   with any C compiler */

main ()
{
  float n[4];
  void get_24();

  printf ("Please enter four numbers: ");
  scanf ("%f%f%f%f", &n[0], &n[1], &n[2], &n[3]);
  while ( n[0] != -1 )
  {
    get_24(n);
    printf ("Please enter four numbers: ");
    scanf ("%f%f%f%f", &n[0], &n[1], &n[2], &n[3]);
  }
}

void get_24(n)
float n[];
{
  int i0, i1, i2, i3, j0, j1, j2, k;
  float op(), tmp_1, tmp_2, result;
  static char z[4] = {'+', '-', '*', '/'};
  void counter();

  k = 0;
  for (i0 = 0; i0 < 4; ++i0)
    for (i1 = 0; i1 < 4; ++i1)
      if (i1 != i0)
        for (i2 = 0; i2 < 4; ++i2)
          if (i2 != i0 && i2 != i1)
            for (i3 = 0; i3 < 4; ++ i3)
	      if (i3 != i0 && i3 != i1 && i3 != i2)
                for (j0 = 0; j0 < 4; ++j0)
                  for (j1 = 0; j1 < 4; ++j1)
                    for (j2 = 0; j2 < 4; ++j2)
                    {
                      tmp_1 = op(j0, n[i0], n[i1]);
                      tmp_2 = op(j1, tmp_1, n[i2]);
                      result = op(j2, tmp_2, n[i3]) - 24.0;
                      if (result < 1.0e-4 && result > -1.0e-4)
                      {
                        printf ("\n((%.0f %c %.0f) %c %.0f) %c %.0f = 24\n",
                                n[i0], z[j0], n[i1], z[j1], n[i2], z[j2],
                                n[i3]);
                        return;
                      }
                      tmp_2 = op(j2, n[i2], n[i3]);
                      result = op(j1, tmp_1, tmp_2) - 24.0;
                      if (result < 1.0e-4 && result > -1.0e-4)
                      {
                        printf ("\n(%.0f %c %.0f) %c (%.0f %c %.0f) = 24\n",
                                 n[i0], z[j0], n[i1], z[j1], n[i2], z[j2],
                                 n[i3]);
                        return;
                      }
                      tmp_1 = op(j1, n[i1], n[i2]);
                      tmp_2 = op(j0, n[i0], tmp_1);
                      result = op(j2, tmp_2, n[i3]) - 24.0;
                      if (result < 1.0e-4 && result > -1.0e-4)
                      {
                        printf ("\n(%.0f %c (%.0f %c %.0f)) %c %.0f = 24\n",
                                n[i0], z[j0], n[i1], z[j1], n[i2], z[j2],
                                n[i3]);
                        return;
                      }
                      tmp_2 = op(j2, tmp_1, n[i3]);
                      result = op(j0, n[i0], tmp_2) - 24.0;
                      if (result < 1.0e-4 && result > -1.0e-4)
                      {
                        printf ("\n%.0f %c ((%.0f %c %.0f) %c %.0f) = 24\n",
                                n[i0], z[j0], n[i1], z[j1],n[i2], z[j2], 
                                n[i3]);
                        return;
                      }
                      tmp_1 = op(j2, n[i2], n[i3]);
                      tmp_2 = op(j1, n[i1], tmp_1);
                      result = op(j0, n[i0], tmp_2) - 24.0;
                      if (result < 1.0e-4 && result > -1.0e-4)
                      {
                        printf ("\n%.0f %c (%.0f %c (%.0f %c %.0f)) = 24\n",
                                n[i0], z[j0], n[i1], z[j1], n[i2], z[j2],
                                n[i3]);
                        return;
                      }
                    }
}

float op(j, a, b)
float a, b;
int j;
{
  switch (j)
  {
    case 0: return (a + b);
    case 1: return (a - b);
    case 2: return (a * b);
    case 3: if (b == 0.0)
            {
              return (1.0e6);
            }
            else
            {
              return (a / b);
            }
  }
}
Newsgroups: soc.culture.china