回 帖 发 新 帖 刷新版面

主题:算24点全集之程序

#include <iostream.h>
#include <string.h>
int get1(int (*p1)[4],char (*p2)[3])
{  int temp;
    switch((*p2)[0])
    {    case 0: temp=(*p1)[0]+(*p1)[1]; break;
case 1: temp=(*p1)[0]-(*p1)[1]; break;
case 2: temp=(*p1)[0]*(*p1)[1]; break;
case 3: if ((*p1[1])&&((*p1)[0]%(*p1)[1]==0))
{    temp=(*p1)[0]/(*p1)[1];
      break;
}
else return(0);
    }
    switch((*p2)[1])
    {    case 0: temp+=(*p1)[2]; break;
case 1: temp-=(*p1)[2]; break;
case 2: temp*=(*p1)[2]; break;
case 3: if ((*p1)[2]&&(temp%(*p1)[2]==0))
{    temp=temp/(*p1)[2];
      break;
  }
  else return(0);
    }
      switch((*p2)[2])
    {    case 0: temp+=(*p1)[3]; break;
case 1: temp-=(*p1)[3]; break;
case 2: temp*=(*p1)[3]; break;
case 3: if ((*p1)[3]&&(temp%(*p1)[3]==0))
{    temp=temp/(*p1)[3];
      break;
  }
  else return(0);
    }
    return(temp);
}
int get2(int (*p1)[4],char (*p2)[3])
{  int temp;
    switch((*p2)[1])
    {    case 0: temp=(*p1)[1]+(*p1)[2]; break;
case 1: temp=(*p1)[1]-(*p1)[2]; break;
case 2: temp=(*p1)[1]*(*p1)[2]; break;
case 3: if ((*p1)[2]&&((*p1)[1]%(*p1)[2]==0))
{    temp=(*p1)[1]/(*p1)[2];
      break;
}
else return(0);
    }
    switch((*p2)[0])
    {    case 0: temp=(*p1)[0]+temp; break;
case 1: temp=(*p1)[0]-temp; break;
case 2: temp=(*p1)[0]*temp; break;
case 3: if ((temp)&&((*p1)[0]%temp==0))
{    temp=(*p1)[0]/temp;
      break;
  }
  else return(0);
    }
      switch((*p2)[2])
    {    case 0: temp+=(*p1)[3]; break;
case 1: temp-=(*p1)[3]; break;
case 2: temp*=(*p1)[3]; break;
case 3: if ((*p1)[3]&&(temp%(*p1)[3]==0))
{    temp=temp/(*p1)[3];
      break;
  }
  else return(0);
    }
    return(temp);
}
int get3(int (*p1)[4],char (*p2)[3])
{  int temp;
    switch((*p2)[1])
    {    case 0: temp=(*p1)[1]+(*p1)[2]; break;
case 1: temp=(*p1)[1]-(*p1)[2]; break;
case 2: temp=(*p1)[1]*(*p1)[2]; break;
case 3: if ((*p1)[2]&&((*p1)[1]%(*p1)[2]==0))
{    temp=(*p1)[1]/(*p1)[2];
      break;
}
else return(0);
    }
    switch((*p2)[2])
    {    case 0: temp+=(*p1)[3]; break;
case 1: temp-=(*p1)[3]; break;
case 2: temp*=(*p1)[3]; break;
case 3: if ((*p1)[3]&&(temp%(*p1)[3]==0))
{    temp=temp/(*p1)[3];
      break;
  }
  else return(0);
    }
      switch((*p2)[0])
    {    case 0: temp=(*p1)[0]+temp; break;
case 1: temp=(*p1)[0]-temp; break;
case 2: temp=(*p1)[0]*temp; break;
case 3: if ((temp)&&(*p1)[0]%temp==0)
{    temp=(*p1)[0]/temp;
      break;
  }
  else return(0);
    }
    return(temp);
}
int get4(int (*p1)[4],char (*p2)[3])
{  int temp;
    switch((*p2)[2])
    {    case 0: temp=(*p1)[2]+(*p1)[3]; break;
case 1: temp=(*p1)[2]-(*p1)[3]; break;
case 2: temp=(*p1)[2]*(*p1)[3]; break;
case 3: if ((*p1)[3]&&((*p1)[2]%(*p1)[3]==0))
{    temp=(*p1)[2]/(*p1)[3];
      break;
}
else return(0);
    }
    switch((*p2)[1])
    {    case 0: temp=(*p1)[1]+temp; break;
case 1: temp=(*p1)[1]-temp; break;
case 2: temp=(*p1)[1]-temp; break;
case 3: if ((temp)&&(*p1)[1]%temp==0)
{    temp=(*p1)[1]/temp;
      break;
  }
  else return(0);
    }
      switch((*p2)[0])
    {    case 0: temp=(*p1)[0]+temp; break;
case 1: temp=(*p1)[0]-temp; break;
case 2: temp=(*p1)[0]*temp; break;
case 3: if ((temp)&&(*p1)[0]==0)
{    temp=(*p1)[0]/temp;
      break;
  }
  else return(0);
    }
    return(temp);
}
int get5(int (*p1)[4],char (*p2)[3])
{  int tmp1,tmp2;
    switch((*p2)[0])
    {    case 0: tmp1=(*p1)[0]+(*p1)[1]; break;
case 1: tmp1=(*p1)[0]-(*p1)[1]; break;
case 2: tmp1=(*p1)[0]*(*p1)[1]; break;
case 3: if ((*p1)[1]&&((*p1)[0]%(*p1)[1]==0))
{    tmp1=(*p1)[0]/(*p1)[1];
      break;
}
else return(0);
    }
    switch((*p2)[2])
    {    case 0: tmp2=(*p1)[2]+(*p1)[3]; break;
case 1: tmp2=(*p1)[2]-(*p1)[3]; break;
case 2: tmp2=(*p1)[2]*(*p1)[3]; break;
case 3: if ((*p1)[3]&&((*p1)[2]%(*p1)[3]==0))
{    tmp2=(*p1)[2]/(*p1)[3];
      break;
  }
  else return(0);
    }
      switch((*p2)[1])
    {    case 0: tmp1+=tmp2; break;
case 1: tmp1-=tmp2; break;
case 2: tmp1*=tmp2; break;
case 3: if ((tmp2)&&(tmp1%tmp2==0))
{    tmp1/=tmp2;
      break;
  }
  else return(0);
    }
    return(tmp1);
}
void main()
{    int num[24][4],(*pp1)[4],input[4],goal,i,j,k,m,n;
    char (*pp2)[3],code[64][3];
    cout<<"Goal=";
    cin>>goal;
    cout<<"Input four numbers:";
    for(i=0;i<4;i++)  cin>>input[i];
    m=0;
    for(i=0;i<4;i++)
    for(j=0;j<4;j++)
    for(k=0;k<4;k++)
    {    code[m][0]=i;
  code[m][1]=j;
  code[m][2]=k;
  m++;
    }
    n=0;
    for(i=0;i<4;i++)
    for(j=0;j<4;j++)
    if (j!=i) for(k=0;k<4;k++)
      if ((k!=i)&&(k!=j)) {  m=6-i-j-k;
      num[n][0]=input[i];
      num[n][1]=input[j];
      num[n][2]=input[k];
      num[n][3]=input[m];
      n++;
  }
    m=0;
    for(i=0,pp1=num;i<24;i++,pp1++)
    {    for(j=0,pp2=code;j<64;j++,pp2++)
{  k=get1(pp1,pp2); if (k==goal) {m=1; break;}
    k=get2(pp1,pp2); if (k==goal) {m=2; break;}
    k=get3(pp1,pp2); if (k==goal) {m=3; break;}
    k=get4(pp1,pp2); if (k==goal) {m=4; break;}
    k=get5(pp1,pp2); if (k==goal) {m=5; break;}
}
if(m) break;
    }
    if(m)
    {    char analyst[12];
  for(i=0;i<3;i++)
  switch((*pp2)[i])
  {    case 0:(*pp2)[i]='+'; break;
      case 1:(*pp2)[i]='-'; break;
      case 2:(*pp2)[i]='*'; break;
      case 3:(*pp2)[i]='/';
  }
  switch (m)
  {    //The following may be hard to understand!
      //See how and why to do in the end of the program!
      // ( ( a ? b ) ? c ) ? d          ?=+,-,*,/
      // 0 1 2 3 4 5 6 7 8 9 10
      case 1:strcpy(analyst,"((a?b)?c)?d");
      analyst[3]=(*pp2)[0];
      analyst[6]=(*pp2)[1];
      analyst[9]=(*pp2)[2];
      if((analyst[3]=='*')||(analyst[3]=='/')||
(analyst[6]=='+')||(analyst[6]=='-'))
      {  analyst[1]='0';    analyst[5]='0';  }
      if((analyst[6]=='*')||(analyst[6]=='/')||
(analyst[9]=='+')||(analyst[9]=='-'))
      {  analyst[0]='0';    analyst[8]='0';  }
      break;
      // ( a ? ( b ? c ) ) ? d
      // 0 1 2 3 4 5 6 7 8 9 10
      case 2:strcpy(analyst,"(a?(b?c))?d");
      analyst[2]=(*pp2)[0];
      analyst[5]=(*pp2)[1];
      analyst[9]=(*pp2)[2];
      if((analyst[2]=='+')||(analyst[2]!='/')&&
((analyst[5]=='*')||(analyst[5]=='/')))
      {  analyst[3]='0';    analyst[7]='0';  }
      if((analyst[2]=='*')||(analyst[2]=='/')||
(analyst[9]=='+')||(analyst[9]=='-'))
      {  analyst[0]='0';    analyst[8]='0';  }
      break;
      // a ? ( ( b ? c ) ? d )
      // 0 1 2 3 4 5 6 7 8 9 10
      case 3:strcpy(analyst,"a?((b?c)?d");
      analyst[1]=(*pp2)[0];
      analyst[5]=(*pp2)[1];
      analyst[8]=(*pp2)[2];
      if((analyst[5]=='*')||(analyst[5]=='/')||
(analyst[8]=='+')||(analyst[8]=='-'))
      {  analyst[3]='0';    analyst[7]='0';  }
      if((analyst[1]=='+')||(analyst[1]!='/')&&
((analyst[8]=='*')||(analyst[8]=='/')))
      {  analyst[2]='0';    analyst[10]='0';  }
      break;
      // a ? ( b ? ( c ? d ) )
      // 0 1 2 3 4 5 6 7 8 9 10
      case 4:strcpy(analyst,"a?(b?(c?d))");
      analyst[1]=(*pp2)[0];
      analyst[4]=(*pp2)[1];
      analyst[7]=(*pp2)[2];
      if((analyst[4]=='+')||(analyst[4]!='/')&&
((analyst[7]=='*')||(analyst[7]=='/')))
      {  analyst[5]='0';    analyst[9]='0';  }
      if((analyst[1]=='+')||(analyst[1]!='/')&&
((analyst[4]=='*')||(analyst[4]=='/')))
      {  analyst[2]='0';    analyst[10]='0';  }
      break;
      // ( a ? b ) ? ( c ? d )
      // 0 1 2 3 4 5 6 7 8 9 10
      case 5:strcpy(analyst,"(a?b)?(c?d)");
      analyst[2]=(*pp2)[0];
      analyst[5]=(*pp2)[1];
      analyst[8]=(*pp2)[2];
      if((analyst[2]=='*')||(analyst[2]=='/')||
(analyst[5]=='+')||(analyst[5]=='-'))
      {  analyst[0]='0';    analyst[4]='0';  }
      if((analyst[5]=='+')||(analyst[5]!='/')&&
((analyst[8]=='*')||(analyst[8]=='/')))
      {  analyst[6]='0';    analyst[10]='0';  }
  }
  for(i=0;i<11;i++)
  switch(analyst[i])
  {    case 'a': cout<<(*pp1)[0]; break;
      case 'b': cout<<(*pp1)[1]; break;
      case 'c': cout<<(*pp1)[2]; break;
      case 'd': cout<<(*pp1)[3]; break;
      case '0': break;
      default:  cout<<analyst[i];
  }
  cout<<endl;
    }
    else
    {    for(i=0,pp1=num;i<24;i++,pp1++)
{    j=(*pp1)[0]*(*pp1)[1];
      k=(*pp1)[0]*(*pp1)[2];
              if(k%(*pp1)[3]==0)
              {    k/=(*pp1)[3];
                    if(j+k==goal) {    m=1;  break;  }
                    else if(j-k==goal) {    m=2;  break;  }
else if(k-j==goal) {    m=3;  break;    }
              }
      else
              {    j=(*pp1)[0]*(*pp1)[3];
                    k=(*pp1)[1]*(*pp1)[3];
                    n=(*pp1)[2];
    if((k+n)&&(j%(k+n)==0))
    {    if(j/(k+n)==goal)
{    m=4;  break;    }
                    }
                    else
    if((k-n)&&(j%(k-n)==0))
                    if(j/(k-n)==goal)  {    m=5;  break;  }
                    else if(j/(n-k)==goal) {    m=6;  break;    }
              }
        }
if(!m) cout<<"If you get it,please E-mail to:publicfrk@netease.com\n";
else  switch (m)
      {    case 1:cout<<(*pp1)[0]<<"*("<<(*pp1)[1]<<'+'
  <<(*pp1)[2]<<'/'<<(*pp1)[3]<<')'<<endl;
  break;
    case 2:cout<<(*pp1)[0]<<"*("<<(*pp1)[1]<<'-'
  <<(*pp1)[2]<<'/'<<(*pp1)[3]<<')'<<endl;
  break;
    case 3:cout<<(*pp1)[0]<<"*("<<(*pp1)[1]<<'/'
  <<(*pp1)[2]<<'-'<<(*pp1)[3]<<')'<<endl;
  break;
    case 4:cout<<(*pp1)[0]<<"/("<<(*pp1)[1]<<'+'
  <<(*pp1)[2]<<'/'<<(*pp1)[3]<<')'<<endl;
  break;
    case 5:cout<<(*pp1)[0]<<"/("<<(*pp1)[1]<<'-'
  <<(*pp1)[2]<<'/'<<(*pp1)[3]<<')'<<endl;
  break;
    case 6:cout<<(*pp1)[0]<<"/("<<(*pp1)[1]<<'/'
  <<(*pp1)[2]<<'-'<<(*pp1)[3]<<')'<<endl;
}
    }
}
// .T. means you can get ride of ( )
//      a?(b?c) (a?b)?c
//              +  +    .T.   .T.
// +  -    .T.                              .T.
// +  *    .T.                              .F.
//              +  /    .T.                              .F.
// -  +    .F.                              .T.
//              -  -    .F.                              .T.
// -  *    .T.                              .F.
//         -  /    .T.                              .F.
//              *  +    .F.                              .T.
//              *  -    .F.                              .T.
//              *  *    .T.                              .T.
//              *  /    .T.                              .T.
//              /  +    .F.                              .T.
//              /  -    .F.                              .T.
// /  *    .F.                              .T.
//              /  /    .F.                              .T.
//----------------------------------------------------------------
//Conculsion    +    +,-,*,/                  *,/    +,-,*,/
//            *,-    *,/
  

回复列表 (共8个回复)

沙发

老大,我才学,看不懂 5……………………

板凳

老大呀老大。你用的是什么版?我的没有#include <iostream.h>呀。

3 楼

这是c++语言写的,tc里不能调试!

4 楼

来点注释好不好!?

5 楼

那确实

6 楼

那个,是不是不需要这么长的代码啊?

看我的,呵呵。虽然前两天帖过#include<stdio.h>

#include<conio.h>
void main()
{    int number[4];
    int a,b,c,d,i,j,k,n;
    float temp;
    float cptall(int number[],int a,int b,int c,int d,int i,int j,int k,int n);
    void printall(int number[],int a,int b,int c,int d,int i,int j,int k,int n);
    for(n=0;n<4;n++)
    {    scanf("%d",number+n);
        printf("number[%d]=%d\n",n,number[n]);
    }
    for(a=0;a<4;a++)
        for(b=0;b<4;b++)
        {    if(b==a)continue;
            for(c=0;c<4;c++)
            {    if(c==a || c==b)continue;
                for(d=0;d<4;d++)
                {    if(d==a || d==b || d==c)continue;
                    for(i=0;i<4;i++)
                        for(j=0;j<4;j++)
                            for(k=0;k<4;k++)
                                for(n=0;n<6;n++)
                                {    if((temp=cptall(number,a,b,c,d,i,j,k,n))<24.1 && temp>23.9)
                                    {    printall(number,a,b,c,d,i,j,k,n);
                                        getch();
                                        return;
                                    }
                                }
                }
            }
        }
    printf("No Answer Found.\n");
    getch();
    return;
}
float cptall(int number[],int a,int b,int c,int d,int i,int j,int k,int n)
{   float cpt(float x,float y,int m);
    switch(n)
    {   case 0:return cpt(cpt(cpt(number[a],number[b],i),number[c],j),number[d],k);//1 2 3
        case 1:return cpt(cpt(number[a],number[b],i),cpt(number[c],number[d],k),j);//1 3 2
        case 2:return cpt(cpt(number[a],cpt(number[b],number[c],j),i),number[d],k);//2 1 3
        case 3:return cpt(number[a],cpt(cpt(number[b],number[c],j),number[d],k),i);//2 3 1
        case 4:return cpt(cpt(number[a],number[b],i),cpt(number[c],number[d],k),j);//3 1 2
        case 5:return cpt(number[a],cpt(number[b],cpt(number[c],number[d],k),j),i);//3 2 1
        default:return 0;
    }
}
float cpt(float x,float y,int m)
{   switch(m)
    {    case 0:return x+y;
        case 1:return x-y;
        case 2:return x*y;
        case 3:    if(y<0.1 && y>-0.1)return 0;    else return float(x)/float(y);
        default:return 0;
    }
}
void printall(int number[],int a,int b,int c,int d,int i,int j,int k,int n)
{   char op1,op2,op3;
    op1=(i==0?'+':(i==1?'-':(i==2?'*':'/')));
    op2=(j==0?'+':(j==1?'-':(j==2?'*':'/')));
    op3=(k==0?'+':(k==1?'-':(k==2?'*':'/')));
    switch(n)
    {    case 0:printf("24=[(%d%c%d)%c%d]%c%d\n",number[a],op1,number[b],op2,number[c],op3,number[d]);break;
        case 1:printf("24=(%d%c%d)%c(%d%c%d)\n",number[a],op1,number[b],op2,number[c],op3,number[d]);break;
        case 2:printf("24=[%d%c(%d%c%d)]%c%d\n",number[a],op1,number[b],op2,number[c],op3,number[d]);break;
        case 3:printf("24=%d%c[(%d%c%d)%c%d]\n",number[a],op1,number[b],op2,number[c],op3,number[d]);break;
        case 4:printf("24=(%d%c%d)%c(%d%c%d)\n",number[a],op1,number[b],op2,number[c],op3,number[d]);break;
        case 5:printf("24=%d%c[%d%c(%d%c%d)]\n",number[a],op1,number[b],op2,number[c],op3,number[d]);break;
        default:printf("error(%d).\n",n);break;
    }
}一次了。

7 楼

楼上的!!你的那么多的for语句!!运行实效也太欠缺了吧!!

8 楼

大哥,运行时发现
5 5 5 5 
(5+5×5)/5
5  5  5  1
5+5-5+1
4 4 4 4
4+(4+4)×4
```````
这个时怎么回事阿

我来回复

您尚未登录,请登录后再回复。点此登录或注册