- 注册时间
- 2008-2-6
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 51573
- 在线时间
- 小时
|
发表于 2008-8-4 16:02:40
|
显示全部楼层
重新写的求值函数- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
-
- #define max 16
- #define nmax 64 * 1024 * 1024
-
- int calc[2 * max - 1];
- int operator[4] = { -1, -3, -2, -4 }; // + * - /
- int mask[max + 1]; //代表数字是否被使用
- int nmask[nmax]; //表示的数字
- int n;
- int leaf; //剩余数字
- int position;
- int counter;
-
- void
- init (void)
- {
- int i;
- for (i = 1; i <= max; i++)
- mask[i] = 0;
- position = 0;
- counter = 0;
- leaf = n;
- memset(nmask, 0, nmax * sizeof(int));
- }
-
- void search(void)
- {
- int i;
- for (i = 1; i < nmax; i ++)
- if (nmask[i] == 0)
- {
- printf("\nmin: %d\n", i);
- break;
- }
- }
-
- //返回0代表表达式合法,-1代表除法不能除尽,-2代表除以0,-3代表结果为负
- int print(void)
- {
- int i;
- int opTop = 0, numTop = 0;
- long long temp;
- long long numBuffer[max];
- int itemp;
-
- for (i = 2 * n - 2; i >= 0; i --)
- {
- if (calc[i] < 0)
- {
- switch (calc[i])
- {
- case -1:
- temp = numBuffer[numTop - 1] + numBuffer[numTop - 2];
- break;
- case -2:
- temp = numBuffer[numTop - 1] - numBuffer[numTop - 2];
- break;
- case -3:
- temp = numBuffer[numTop - 1] * numBuffer[numTop - 2];
- break;
- case -4:
- if (numBuffer[numTop - 2] == 0)
- return (-2);
- if (numBuffer[numTop - 1] % numBuffer[numTop - 2] > 0)
- return (-1);
- temp = numBuffer[numTop - 1] / numBuffer[numTop - 2];
- break;
- }
- numTop -= 1;
- numBuffer[numTop - 1] = temp;
- }
- else
- numBuffer[numTop ++] = calc[i];
- }
-
- temp = numBuffer[0];
- if (temp < 0) return (-3);
- if (temp < nmax)
- {
- itemp = temp;
- if (nmask[itemp] == 0)
- {
- counter++;
- printf("%d: %d ", counter, itemp);
- for (i = 0; i <= 2 * n - 2; i++)
- {
- if (calc[i] < 0)
- {
- switch (calc[i])
- {
- case -1:
- printf ("+ ");
- break;
- case -2:
- printf ("- ");
- break;
- case -3:
- printf ("* ");
- break;
- case -4:
- printf ("/ ");
- break;
- }
- }
- else
- printf ("%d ", calc[i]);
- }
- printf ("\n");
-
- nmask[itemp] = 1;
- }
- }
-
- return (0);
- }
-
- int
- printOnly (void)
- {
- int i;
- for (i = 0; i <= 2 * n - 2; i++)
- {
- if (calc[i] < 0)
- {
- switch (calc[i])
- {
- case -1:
- printf ("+ ");
- break;
- case -2:
- printf ("- ");
- break;
- case -3:
- printf ("* ");
- break;
- case -4:
- printf ("/ ");
- break;
- }
- }
- else
- printf ("%d ", calc[i]);
- }
- printf ("\n");
- counter++;
- }
-
- void
- printBlank (void)
- {
- counter++;
- }
-
- int
- top (int MaxNode)
- {
- int i;
- if (MaxNode >= 1)
- for (i = 0; i < 4; i++)
- {
- calc[position++] = operator[i];
- if (i < 2)
- subNode (MaxNode - 1, 1);
- else
- subNode (MaxNode - 1, 0);
- position--;
- }
- }
-
- void
- addmul (void)
- {
- int i, j;
- for (i = 1; i <= n - 1; i++)
- if (mask[i] == 0)
- {
- mask[i] = 1;
- leaf--;
- calc[position++] = i;
- for (j = i + 1; j <= n; j++)
- if (mask[j] == 0)
- {
- calc[position++] = j;
- mask[j] = 1;
- leaf--;
- if (leaf == 0)
- {
- // check();
- print ();
- }
- mask[j] = 0;
- leaf++;
- position--;
- }
- leaf++;
- position--;
- mask[i] = 0;
- }
- }
-
- void
- subdiv (void)
- {
- int i, j;
- for (i = 1; i <= n; i++)
- if (mask[i] == 0)
- {
- mask[i] = 1;
- leaf--;
- calc[position++] = i;
- for (j = 1; j <= n; j++)
- if (mask[j] == 0)
- {
- calc[position++] = j;
- mask[j] = 1;
- leaf--;
- if (leaf == 0)
- {
- // check();
- print ();
- }
- mask[j] = 0;
- leaf++;
- position--;
- }
- leaf++;
- position--;
- mask[i] = 0;
- }
- }
-
- void
- leftFill (int MaxNode)
- {
- int i;
- for (i = 1; i <= n; i++)
- if (mask[i] == 0)
- {
- calc[position++] = i;
- leaf--;
- mask[i] = 1;
- top (MaxNode);
- position--;
- leaf++;
- mask[i] = 0;
- }
- }
-
- void
- rightFill (int MaxNode)
- {
- int i;
- for (i = 1; i <= n; i++)
- if (mask[i] == 0)
- {
- mask[i] = 1;
- calc[position + 2 * MaxNode + 1] = i;
- leaf--;
- top (MaxNode);
- leaf++;
- mask[i] = 0;
- }
- }
-
- int
- subNode (int MaxNode, int exchange)
- {
- int i, j;
- if (MaxNode == 0)
- {
- if (exchange == 1)
- addmul ();
- else
- subdiv ();
- }
-
- if (MaxNode >= 1)
- {
- //1\fill left
- leftFill (MaxNode);
- //2\fill right
- if (exchange == 0)
- rightFill (MaxNode);
- }
-
- //3\top(left) top(right)
- if (MaxNode >= 2)
- for (i = 1; i < MaxNode; i++)
- {
- top (i);
- top (MaxNode - i);
- }
- }
-
- int
- main (void)
- {
- struct timeval start, end;
- double timeuse;
- printf ("Input n value:");
- scanf ("%d", &n);
- printf ("\n\n");
- gettimeofday (&start, NULL);
- init ();
- top (n - 1);
- printf ("\nTotal: %d\n", counter);
- search();
- gettimeofday (&end, NULL);
- timeuse =
- 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
- timeuse /= 1000000;
- printf ("\n\nUsed Time:%8.6f\n\n", timeuse);
- return 0;
- }
复制代码 n <=5和mathe的答案一致
从6开始就不对了
在查找原因
[ 本帖最后由 无心人 于 2008-8-4 21:48 编辑 ] |
|