回复 楼主 a396693980
写了一个,不知道能不能用。
它应该能列出所有的公式。但不是太智能。很多重了的公式,可能它并不去过滤。
比如
数字一 + 数字二 + 2
和
数字二 + 数字一 + 2
它会认为是两个不一样的公式,虽然逻辑应该是一样的。
代码如下:
程序代码:
#include <stdio.h>
int a[3][7];
int formula1( int a, int trt, int opt, int n )
{
int res;
switch (trt) {
case 1: a %= 10; break;
case 2: a /= 10; break;
case 3: a = a/10 + a%10; break;
case 4: /* a = a; */ break;
default: return -1;
}
switch (opt) {
case 0: res = a; break;
case 1: res = a + n; break;
case 2: res = a - n; break;
}
return res; // No invalid formula can be generate.
}
int formula2( int a, int trta, int b, int trtb, int opt, int n )
{
int res;
int cplus = 0, cminus = 0;
switch (trta) {
case 1: a %= 10; break;
case 2: a /= 10; break;
case 3: a = a/10 + a%10;
++cplus; break;
case 4: /* a = a; */ break;
default: return -1;
}
switch (trtb) {
case 1: b %= 10; break;
case 2: b /= 10; break;
case 3: b = b/10 + b%10;
++cplus; break;
case 4: /* b = b; */ break;
case 5: b = -(b%10);
++cminus; --cplus; break;
case 6: b = -(b/10);
++cminus; --cplus; break;
case 7: b = -b;
++cminus; --cplus; break;
default: return -1;
}
switch (opt) {
case 0: res = a + b;
++cplus; break;
case 1: res = a + b + n;
cplus += 2; break;
case 2: res = a + b - n;
++cplus, ++cminus; break;
}
return (cplus <=3 && cminus <= 1) ? res : -1;
}
int formula3( int a, int trta, int b, int trtb, int c, int trtc, int opt, int n )
{
int res;
int cplus = 0, cminus = 0;
switch (trta) {
case 1: a %= 10; break;
case 2: a /= 10; break;
case 3: a = a/10 + a%10;
++cplus;break;
case 4: /* a = a; */ break;
default: return -1;
}
switch (trtb) {
case 1: b %= 10; break;
case 2: b /= 10; break;
case 3: b = b/10 + b%10;
++cplus; break;
case 4: /* b = b; */ break;
case 5: b = -(b%10);
++cminus; --cplus; break;
case 6: b = -(b/10);
++cminus; --cplus; break;
case 7: b = -b;
++cminus; --cplus; break;
default: return -1;
}
switch (trtc) {
case 1: c %= 10; break;
case 2: c /= 10; break;
case 3: c = c/10 + c%10;
++cplus; break;
case 4: /* c = c; */ break;
case 5: c = -(c%10);
++cminus; --cplus; break;
case 6: c = -(c/10);
++cminus; --cplus; break;
case 7: c = -c;
++cminus; --cplus; break;
default: return -1;
}
switch (opt) {
case 0: res = a + b + c;
cplus += 2; break;
case 1: res = a + b + c + n;
cplus += 3; break;
case 2: res = a + b + c - n;
cplus += 2, ++cminus; break;
}
return (cplus <=3 && cminus <= 1) ? res : -1;
}
int formula4( int a, int trta, int b, int trtb,
int c, int trtc, int d, int trtd, int opt, int n )
{
int res;
int cplus = 0, cminus = 0;
switch (trta) {
case 1: a %= 10; break;
case 2: a /= 10; break;
case 3: a = a/10 + a%10;
++cplus;break;
case 4: /* a = a; */ break;
default: return -1;
}
switch (trtb) {
case 1: b %= 10; break;
case 2: b /= 10; break;
case 3: b = b/10 + b%10;
++cplus; break;
case 4: /* b = b; */ break;
case 5: b = -(b%10);
++cminus; --cplus; break;
case 6: b = -(b/10);
++cminus; --cplus; break;
case 7: b = -b;
++cminus; --cplus; break;
default: return -1;
}
switch (trtc) {
case 1: c %= 10; break;
case 2: c /= 10; break;
case 3: c = c/10 + c%10;
++cplus; break;
case 4: /* c = c; */ break;
case 5: c = -(c%10);
++cminus; --cplus; break;
case 6: c = -(c/10);
++cminus; --cplus; break;
case 7: c = -c;
++cminus; --cplus; break;
default: return -1;
}
switch (trtd) {
case 1: d %= 10; break;
case 2: d /= 10; break;
case 3: d = d/10 + d%10;
++cplus; break;
case 4: /* d = d; */ break;
case 5: d = -(d%10);
++cminus; --cplus; break;
case 6: d = -(d/10);
++cminus; --cplus; break;
case 7: d = -d;
++cminus; --cplus; break;
default: return -1;
}
switch (opt) {
case 0: res = a + b + c + d;
cplus += 3; break;
case 1: res = a + b + c + d + n;
cplus += 4; break;
case 2: res = a + b + c + d - n;
cplus += 3, ++cminus; break;
}
return (cplus <=3 && cminus <= 1) ? res : -1;
}
int check(a1, a2)
{
return (a1%12 == a[1][6]%12)
&& (a2%12 == a[2][6]%12);
}
const char st[] = { '@', 'l', 'h', 's', 'n', 'l', 'h', 'n', '@' };
const char sp[] = { '#', '+', '-', '@' };
void showf1(int i, int t, int p, int n)
{
if (p != 0)
printf("Formula: %d%c %c %d\n", i+1, st[t], sp[p], n);
}
void showf2(int i, int t, int j, int tt, int p, int n)
{
printf("Formula: %d%c %c %d%c", i+1, st[t], (tt<5 ? '+' : '-'), j+1, st[tt]);
if (p != 0) printf(" %c %d\n", sp[p], n);
else printf("\n");
}
void showf3(int i, int t, int j, int tt, int k, int t3, int p, int n)
{
printf("Formula: %d%c %c %d%c %c %d%c", i+1, st[t],
(tt<5 ? '+' : '-'), j+1, st[tt], (t3<5 ? '+' : '-'), k+1, st[t3]);
if (p != 0) printf(" %c %d\n", sp[p], n);
else printf("\n");
}
void showf4(int i, int t, int j, int tt, int k, int t3,
int l, int t4, int p, int n)
{
printf("Formula: %d%c %c %d%c %c %d%c %c %d%c", i+1, st[t],
(tt<5 ? '+' : '-'), j+1, st[tt], (t3<5 ? '+' : '-'), k+1, st[t3],
(t4<5 ? '+' : '-'), l+1, st[t4]);
if (p != 0) printf(" %c %d\n", sp[p], n);
else printf("\n");
}
int generate()
{
int count = 0;
int i, t, p, n;
for (i = 0; i < 7; i++)
for (t = 1; t <= 4; t++)
for (p = 0; p <= 2; p++)
for (n = 1; n <= 10; n++)
{
if ( check(formula1(a[0][i], t, p, n), formula1(a[1][i], t, p, n)) )
{ showf1(i, t, p, n); ++count; }
if (p == 0) break;
}
int j, tt;
for (i = 0; i < 7; i++)
for (t = 1; t <= 4; t++)
for (j = 0; j < 7; j++)
for (tt = 1; tt <= 7; tt++)
for (p = 0; p <= 2; p++)
for (n = 1; n <= 10; n++)
{
if ( check(formula2(a[0][i], t, a[0][j], tt, p, n),
formula2(a[1][i], t, a[1][j], tt, p, n)) )
{ showf2(i, t, j, tt, p, n); ++count; }
if (p == 0) break;
}
int k, t3;
for (i = 0; i < 7; i++)
for (t = 1; t <= 4; t++)
for (j = 0; j < 7; j++)
for (tt = 1; tt <= 7; tt++)
for (k = 0; k < 7; k++)
for (t3 = 1; t3 <= 7; t3++)
for (p = 0; p <= 2; p++)
for (n = 1; n <= 10; n++)
{
if ( check(formula3(a[0][i], t, a[0][j], tt, a[0][k], t3, p, n),
formula3(a[1][i], t, a[1][j], tt, a[1][k], t3, p, n)) )
{ showf3(i, t, j, tt, k, t3, p, n); ++count; }
if (p == 0) break;
}
int l, t4;
for (i = 0; i < 7; i++)
for (t = 1; t <= 4; t++)
for (j = 0; j < 7; j++)
for (tt = 1; tt <= 7; tt++)
for (k = 0; k < 7; k++)
for (t3 = 1; t3 <= 7; t3++)
for (l = 0; l < 7; l++)
for (t4 = 1; t4 <= 7; t4++)
for (p = 0; p <= 2; p++)
for (n = 1; n <= 10; n++)
{
if ( check(formula4(a[0][i], t, a[0][j], tt, a[0][k], t3, a[0][l], t4, p, n),
formula4(a[1][i], t, a[1][j], tt, a[1][k], t3, a[1][l], t4, p, n)) )
{ showf4(i, t, j, tt, k, t3, l, t4, p, n); ++count; }
if (p == 0) break;
}
return count;
}
void get()
{
int i, j;
for(i = 0; i < 3; i++)
for(j = 0; j < 7; j++)
scanf("%d", &a[i][j]);
}
int main(int argc, char *argv[])
{
int count;
get();
count = generate();
printf("======> total count: %d <======\n", count);
return 0;
}