由算24点引出的问题
先规定:只允许使用正整数范围内进行的四则运算。开局可用的数字是1~13。问题:如果对方给出1~13中任意两个数字,我方一定能找到使得四个数字组合算得出24的两张牌,也一定能找到使得四个数字算不出24的两张牌,那么我方最少需要多少张牌?
先规定:只允许使用正整数范围内进行的四则运算。开局可用的数字是1~13。
下面的算法是允许的吗?
1÷(2÷6÷8)=24
2÷(1÷3÷4)=24
3÷(1 -7÷8)=24
4÷(7÷6 -1)=24
5×(5-2÷10)=24
6÷(13÷4-3)=24
7×(4 - 4÷7)=24
8÷(4-11÷3)=24
9×(7-13÷3)=24
10×(3-6÷10)=24
11×(2+2÷11)=24
12÷(6÷1÷12)=24
13×(1+11÷13)=24
这个问题计算机穷举即可,复杂度还好,不算太大 7张牌,有17种不同的选择,对于每个对手出的两张牌,后面;隔开两组使用前面两个数无法算出24,使用后面两个可以算出24
2*1 3*1 4*1 8*1 9*1 11*1 13*1
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
2*1 3*1 5*1 8*1 9*1 11*1 13*1
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>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>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
2*1 8*1 9*1 10*1 11*1 12*1 13*1
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
total 17
mathe 发表于 2018-5-30 21:55
7张牌,有17种不同的选择,对于每个对手出的两张牌,后面;隔开两组使用前面两个数无法算出24,使用后面两个 ...
重读此贴,似乎每个数字的牌只用了一次,但事实上是可以重复使用的(比如两张13)。
程序设计是否有误? 的确前面的代码有一个小bug,修正了以后可以找到6个数的三种答案
5*2 7*1 10*1 11*1 13*1
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
5*1 7*2 10*1 11*1 13*1
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
5*1 9*1 10*2 11*1 13*1
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
=>
total 3
代码修改自很早以前一个算24的代码,其中c24函数用于判断给定4个整数是否可以用它们计算出24.
前面的bug是420行里面的for(t=s; 错写成for(t=s+1;了
#include <stdio.h>
#include <map>
#include <algorithm>
using namespace std;
int pm[]={0x1,0x55,0x10515,0x555555,0x41041,0x15,0x30f3f,0xffffff,
0x3f,0x30f3f,0xaaff,0x20b,0xaaff,0x2cb2cb,0x1c71c7};
short table[]={
0, 65, 130, 195, 132, 261, 134, 199,
328, 201, 138, 203, 140, 205, 142, 207,
402, 467, 537, 410, 475, 412, 605, 414,
479, 354, 227, 164, 229, 166, 231, 42,
107, 172, 237, 174, 303, 691, 436, 501,
438, 503, 1416, 1481, 1738, 1803, 1420, 1485,
1871, 1432, 1497, 1754, 1819, 1436, 1501, 1887,
1760, 1825, 1442, 1507, 1893, 1446, 1511, 1776,
1841, 1458, 1523, 1909, 1462, 1527, 2883, 2503,
2899, 2519, 2921, 2541, 2937, 2557, 3331, 3975,
3915, 3535, 3287, 3931, 3551, 3937, 3557, 3369,
4013, 3953, 3573, 3325, 4301, 4573, 4327, 4599
};
short o0;
short o1;
short o2;
short o3;
char opprint[]={'+','-','*','/'};
int mask_cd,mask_bcd,mask_ab_cd;
#define mask_null 0xFFFFFF
#define mask_abcd 1
struct expression{
int value;
expression(int v):value(v){ }
int first_op()const{
return value & 3;
}
int second_op()const{
return (value >> 2)&3;
}
int third_op()const{
return (value >> 4)&3;
}
int graph_type()const{
return (value >> 6)/24;
}
int num_order()const{
return (value >> 6)%24;
}
};
typedef int INT;
struct factor_num{
INT up;
INT down;
factor_num(){}
factor_num(INT u,INT d):up(u),down(d){ }
};
typedef factor_num (*OperatorFun)(const factor_num& x,const factor_num& y);
factor_num sum(const factor_num& i,const factor_num& j){
INT d,u;
if(i.down==0||j.down==0){
d=0;
}else{
d=i.down * j.down;
u=i.up * j.down + j.up * i.down;
}
return factor_num(u,d);
}
factor_num dif(const factor_num& i,const factor_num& j){
INT d,u;
if(i.down==0||j.down==0){
d=0;
}else{
d=i.down * j.down;
u=i.up * j.down - j.up * i.down;
}
return factor_num(u,d);
}
factor_num prod(const factor_num& i,const factor_num& j){
INT d,u;
u=i.up * j.up;
d=i.down * j.down;
return factor_num(u,d);
}
factor_num ratio(const factor_num& i,const factor_num& j){
INT d,u;
if(i.down == 0 || j.down==0){
d=0;
}else{
d=i.down * j.up;
u=i.up * j.down;
}
return factor_num(u,d);
}
OperatorFun funs[]={sum,dif,prod,ratio};
bool equal_num(const factor_num& i,INT j)
{
if(i.down==0){
return false;
}else{
return i.up == j * i.down;
}
}
#define TYPE_MUL_ONE 1
#define TYPE_ADD_SUB 2
#define TYPE_MUL_TWO 4
bool factor24(int d)
{
return d==2||d==3||d==4||d==6||d==8||d==12;
}
#define printf(...)
int show(INT input[],expression expr){
int order=expr.num_order();
int rtype = 0;
INT aa=input];
INT bb=input];
INT cc=input];
INT dd=input];
short op1=expr.first_op();
char ops1=opprint;
short op2=expr.second_op();
char ops2=opprint;
short op3=expr.third_op();
char ops3=opprint;
if(op1<=1&&op2<=1&&op3<=1)rtype=TYPE_ADD_SUB;
if(op3>=2&&dd==1&&op1<=1&&op2<=1)rtype=TYPE_ADD_SUB;
if(op1==2&&aa==1&&op2<=1&&op3<=1)rtype=TYPE_ADD_SUB;
switch(expr.graph_type()){
case 0:
if(op1<2 && op2 >=2){
printf("(%d%c%d)%c",aa,ops1,bb,ops2);
} else {
printf("%d%c%d%c",aa,ops1,bb,ops2);
}
if(op2>=op3){
printf("(%d%c%d)",cc,ops3,dd);
}else{
printf("%d%c%d",cc,ops3,dd);
}
if(op2==2)rtype=TYPE_MUL_TWO;
break;
case 1:
if(op2<2&&op3>=2)
printf("(");
if(op1<2&&op2>=2)
printf("(");
printf("%d%c%d",aa,ops1,bb);
if(op1<2&&op2>=2)
printf(")");
printf("%c%d",ops2,cc);
if(op2<2&&op3>=2)
printf(")");
printf("%c%d",ops3,dd);
if(op3==2&&factor24(dd)){
rtype = TYPE_MUL_ONE;
}else if((op3==2||op3==3)&&dd==1){
if(op2==2&&factor24(cc)){
rtype = TYPE_MUL_ONE;
}
}
break;
case 2:
if(op1<2&&op3>=2)
printf("(");
printf("%d%c",aa,ops1);
if(op1>=op2)
printf("(");
printf("%d%c%d",bb,ops2,cc);
if(op1>=op2)
printf(")");
if(op1<2&&op3>=2)
printf(")");
printf("%c%d",ops3,dd);
if(op1==2&&op3==2){
if(factor24(aa)||factor24(dd)){
rtype = TYPE_MUL_ONE;
}
}
break;
case 3:
printf("%d%c",aa,ops1);
if(op1>=op3)
printf("(");
if(op2<2&&op3>=2)
printf("(");
printf("%d%c%d",bb,ops2,cc);
if(op2<2&&op3>=2)
printf(")");
printf("%c%d",ops3,dd);
if(op1>=op3)
printf(")");
if(op1==2&&factor24(aa)){
rtype = TYPE_MUL_ONE;
}else if(op1==2&&aa==1){
if(op3==2&&factor24(dd)){
rtype = TYPE_MUL_ONE;
}
}
break;
case 4:
printf("%d%c",aa,ops1);
if(op1>=op2)
printf("(");
printf("%d%c",bb,ops2);
if(op2>=op3)
printf("(");
printf("%d%c%d",cc,ops3,dd);
if(op2>=op3)
printf(")");
if(op1>=op2)
printf(")");
if(op1==2&&factor24(aa)){
rtype = TYPE_MUL_ONE;
}else if(op1==2&&aa==1){
if(op2==2&&factor24(bb)){
rtype = TYPE_MUL_ONE;
}
}
break;
}
return rtype;
}
#define elems(x) (sizeof(x)/sizeof(x))
int c_main(INT input[],int mask,INT result)
{
int total=0;
int i;
int rtype=0;
factor_num r1,r2,r;
for(i=0;i<elems(table);i++){
int op=table[ i]&63;
int left=table[ i]>>6;
int g=left>>4;
int pl=left&15;
int pattern=pm&mask;
int j;
for(j=0;j<24;j++){
if(pattern&(1<<j)){
short elem=(j+g*24)*64+op;
expression t(elem);
short op1=t.first_op();
short op2=t.second_op();
short op3=t.third_op();
short gtype=t.graph_type();
short order=t.num_order();
factor_num aa=factor_num(input],1);
factor_num bb=factor_num(input],1);
factor_num cc=factor_num(input],1);
factor_num dd=factor_num(input],1);
OperatorFun fun1=funs;
OperatorFun fun2=funs;
OperatorFun fun3=funs;
switch(gtype){
case 0:
r1=fun1(aa,bb);
r2=fun3(cc,dd);
r=fun2(r1,r2);
break;
case 1:
r1=fun1(aa,bb);
r2=fun2(r1,cc);
r=fun3(r2,dd);
break;
case 2:
r1=fun2(bb,cc);
r2=fun1(aa,r1);
r=fun3(r2,dd);
break;
case 3:
r1=fun2(bb,cc);
r2=fun3(r1,dd);
r=fun1(aa,r2);
break;
case 4:
r1=fun3(cc,dd);
r2=fun2(bb,r1);
r=fun1(aa,r2);
break;
}
if(equal_num(r,result)){
rtype|=show(input,t);
printf("\t");
total++;
}
}
}
}
if(total){
printf("\n");
}
return total;
}
int c24(INT s1,INT s2,INT s3,INT s4,INT r){
INT input;
int i,j;
int num = 0;
input=s1;input=s2;input=s3;input=s4;
for(i=0;i<4;i++){
for(j=i+1;j<4;j++){
if(input<input[ i]){
INT temp=input;
input=input[ i];
input[ i]=temp;
}
}
}
if(input==input){//a==b
if(input!=input){
if(input!=input){//only a==b
INT temp=input;
input=input;
input=temp;
temp=input;
input=input;
input=temp;
num+=c_main(input,mask_cd,r);
}else{//a==b,c==d
num+=c_main(input,mask_ab_cd,r);
}
}else if(input!=input){//a==b==c!=d
INT temp=input;
input=input;
input=temp;
num+=c_main(input,mask_bcd,r);
}else{//a==b==c==d
num+=c_main(input,mask_abcd,r);
}
}else{//a!=b
if(input==input){
if(input!=input){//b==c
INT temp=input;
input=input;
input=temp;
num+=c_main(input,mask_cd,r);
}else{//b==c==d
num+=c_main(input,mask_bcd,r);
}
}else{
if(input==input){//c==d
num+=c_main(input,mask_cd,r);
}else{
num+=c_main(input,mask_null,r);
}
}
}
return num;
}
#define N 13
void init()
{
INT i=0;
short a={0,1,2,3};
do{
o0[ i]=a;
o1[ i]=a;
o2[ i]=a;
o3[ i]=a;
i++;
}while(next_permutation(a,a+4));
for(i=0;i<24;i++){
short inv;
inv]=0;
inv]=1;
inv]=2;
inv]=3;
if(inv<inv){
mask_cd|=(1<<i);
}
if(inv<inv&&inv<inv){
mask_bcd|=(1<<i);
}
if(inv<inv&&inv<inv){
mask_ab_cd|=(1<<i);
}
}
}
int bits(int x){
int b=0,i;
for(i=0;i<32;i++)if(x&(1<<i))b++;
return b;
}
char sname;
char r;
#define TARGET 6
int bestr=-1;
int bc=N+1;
int bit3count(int x)
{
int r=0;
while(x>0){
r+=x%3;x/=3;
}
return r;
}
#undef printf
void test(int x)
{
int i,j,s,t,u1,u2,v1,v2;
int a;
int pf=0,ff=0;
int lbc=0;
for(i=0;i<N;i++){
a=x%3;x/=3;
lbc+=a;
}
for(i=0;i<N;i++)for(j=i;j<N;j++){//provide i & j
pf=ff=0;
for(s=0;s<N;s++)for(t=s;t<N;t++){
if(a==0||a==0)continue;
if(s==t&&a==1)continue;
if(r==0){
u1=s;v1=t;
ff++;
}else{
u2=s;v2=t;
pf++;
}
if(ff>0&&pf>0){
goto endloop1;
}
}
endloop1:
if(pf==0||ff==0)return;
}
for(i=0;i<N;i++){
if(a==1){
printf(" %d*1",i+1);
}else if(a==2){
printf(" %d*2",i+1);
}
}
printf("\n");
for(i=0;i<N;i++)for(j=i;j<N;j++){//provide i & j
pf=ff=0;
for(s=0;s<N;s++)for(t=s;t<N;t++){
if(a==0||a==0)continue;
if(s==t&&a==1)continue;
if(r==0){
u1=s;v1=t;
ff++;
}else{
u2=s;v2=t;
pf++;
}
if(ff>0&&pf>0){
goto endloop2;
}
}
endloop2:
printf("\t[%d,%d]=>[%d,%d;%d,%d]\n",i+1,j+1,u1+1,v1+1,u2+1,v2+1);
}
bestr++;
}
int main()
{
INT i=0,j,k,m;
init();
for(i=1;i<=N;i++)for(j=1;j<=N;j++)for(k=1;k<=N;k++)for(m=1;m<=N;m++){
if(c24(i,j,k,m,24)>0){
r=1;
}
}
printf("start:\n");
for(i=0;i<1594323;i++){
int cbc = bit3count(i);
if(cbc!=TARGET)continue;
test(i);
}
printf("total %d\n",bestr+1);
}
mathe你好,你用的是什么编译的,,,
为什么我复制到VS2012中,执行的时候,出错啊,定位在return factor_num(u,d);
这句话上 chaoshikong 发表于 2018-7-14 17:22
mathe你好,你用的是什么编译的,,,
为什么我复制到VS2012中,执行的时候,出错啊,定位在return fa ...
我测试了下,g++编译器编译没有任何报错,甚至warning信息。这个是C++的构造函数,按值返回对象。C++编译器应该都支持的。你该不会用的是C编译器吧 用vs注意文件名要取cpp而不是c作为后缀
页:
[1]
2