Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Permuting and Combining

Author: Dieter Buerssner

Date: 12:24:52 07/08/04

Go up one level in this thread


Obviously the goto is not needed and looks a bit stupid (comes because I edited
some other code, where it actually added some efficiency).

#include <stdio.h>

#define MAX 20

int main(int argc, char *argv[])
{
  int i, t, n, mx=argc-1;
  int q[MAX];

  if (mx >= MAX)
    return 1;
  for (n=mx; n>=1; n--)
  {
    for (i=0; i<n; i++)
      q[i] = mx-(n-i);
    q[n] = mx+1;  /* Use q[n] for loop end detection */
    /* Generate all remaining combinations of n from mx objects */
    for (;;)
    {
      for (i=0; i<n; i++)
        printf("%s ", argv[q[i]+1]);
      printf("\n");
      if (--q[0] < 0)
      {
        for (i=1; q[i] <= i; i++)
          ; /* Empty loop body */
        if (i>=n) /* Hit the marker at the end? */
          break;
        t = --q[i--];
        do
          q[i] = --t;
        while (--i >= 0);
      }
    }
  }
  printf("none\n");
  return 0;
}

Regards,
Dieter



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.