立即注册
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广州大学城网业务调整

[TC]算24点的程序 [复制链接] qrcode

查看: 6213 | 回复: 1

523066680
发表于: 2009-10-19 07:51:30 | 显示全部楼层

支持相除的时候出现的无理数运算哦~
比如 3 3 7 7  的结果是 (3+3/7)*7   = 24  。解析一下, 之间出现了3/7  于是(3+3/7)*7 = (21+3)/7*7 =24
(我自学的,代码可能有累赘的地方,希望大家多多指教
执行程序见附件。
  1. typedef struct
  2. {
  3.   int fa;
  4.   int fb;
  5. } fraction;
  6. char axb[3];
  7. fraction arr[4];
  8. fraction outvar;
  9. int whox[3];
  10. int overa;
  11. void arrangement(int a[],int n);
  12. void next(fraction a[],int n);
  13. void count(fraction a,int x,fraction b,int n);
  14. main()
  15. {
  16. int i,ma[4];
  17. for (i=0;i<4;i++) arr[i].fb=1;
  18. while (1)
  19. {
  20.   printf("Press [Ctrl+C]=Quit \n");
  21.   printf("Input 4 numbers,such as: 3 3 8 8 : ");
  22.   scanf("%d %d %d %d",&ma[0],&ma[1],&ma[2],&ma[3]);
  23.   overa=0;
  24.   arrangement(ma,4);
  25.   if (overa==0) printf("Not result!\n\n");
  26. }
  27. getch();
  28. }
  29. void arrangement(int a[],int n)
  30. {
  31.   int temp[4];
  32.   int i,j,k;
  33.   if (n==0)
  34.   {
  35.     next(arr,4);
  36.     return;
  37.   }
  38.   for (i=0;i<n;i++)
  39.   {  k=0;
  40.      arr[4-n].fa=a[i];
  41.      for (j=0;j<n;j++) {if (i!=j) temp[k++]=a[j];}
  42.      arrangement(temp,n-1);
  43.   }
  44. }
  45. void next(fraction a[],int n)
  46. {
  47.   int i,j;
  48.   fraction temp[4];
  49.   if ((overa==0)&&(n==1)&&(a[0].fa==24*a[0].fb))
  50.   {printf("\n");
  51.    if (whox[1]==0)
  52.    {
  53.      if (whox[0]==0)
  54.      {printf("[(%d %c %d) %c %d] %c %d",arr[0].fa,axb[0],arr[1].fa,axb[1],arr[2].fa,axb[2],arr[3].fa);}
  55.      else if (whox[0]==1)
  56.      {printf("[%d %c (%d %c %d)] %c %d",arr[0].fa,axb[1],arr[1].fa,axb[0],arr[2].fa,axb[2],arr[3].fa);}
  57.      else if (whox[0]==2)
  58.      {printf("(%d %c %d) %c (%d %c %d)",arr[0].fa,axb[1],arr[1].fa,axb[2],arr[2].fa,axb[0],arr[3].fa);}
  59.    }
  60.    else
  61.    {
  62.      if (whox[0]==0)
  63.      {printf("(%d %c %d) %c (%d %c %d)",arr[0].fa,axb[0],arr[1].fa,axb[2],arr[2].fa,axb[1],arr[3].fa);}
  64.      else if (whox[0]==1)
  65.      {printf("%d %c [(%d %c %d) %c %d]",arr[0].fa,axb[2],arr[1].fa,axb[0],arr[2].fa,axb[1],arr[3].fa);}
  66.      else if (whox[0]==2)
  67.      {printf("%d %c [%d %c (%d %c %d)]",arr[0].fa,axb[2],arr[1].fa,axb[1],arr[2].fa,axb[0],arr[3].fa);}
  68.    }
  69.    printf(" = 24 \n\nPress any key to continue...");
  70.    getch();
  71.    printf("\n\n");
  72.    overa=1;
  73.    return;
  74.   }
  75.   for (i=0;i<n-1;i++)
  76.   {
  77.    whox[4-n]=i;
  78.    for (j=0;j<n;j++){temp[j].fa=a[j].fa;temp[j].fb=a[j].fb;}
  79.    for (j=i+1;j<n-1;j++){temp[j].fa=temp[j+1].fa;temp[j].fb=a[j+1].fb;}
  80.    for (j=0;j<4;j++)
  81.    {
  82.      count(a[i],j,a[i+1],n);
  83.      temp[i].fa=outvar.fa;
  84.      temp[i].fb=outvar.fb;
  85.      if (outvar.fb!=0) next(temp,n-1);
  86.    }
  87.   }
  88. }
  89. void count(fraction a,int x,fraction b,int n)
  90. {
  91.   if (x==0)
  92.   {
  93.     axb[4-n]='+';
  94.     outvar.fa=(a.fa*b.fb)+(b.fa*a.fb);
  95.     outvar.fb=a.fb*b.fb;
  96.   }
  97.   if (x==1)
  98.   {
  99.     axb[4-n]='-';
  100.     outvar.fa=(a.fa*b.fb)-(b.fa*a.fb);
  101.     outvar.fb=a.fb*b.fb;
  102.   }
  103.   if (x==2)
  104.   {
  105.     axb[4-n]='*';
  106.     outvar.fa=a.fa*b.fa;
  107.     outvar.fb=a.fb*b.fb;
  108.   }
  109.   if (x==3)
  110.   {
  111.     axb[4-n]='/';
  112.     outvar.fa=a.fa*b.fb;
  113.     outvar.fb=a.fb*b.fa;
  114.   }
  115. }
复制代码

Count24.rar

9 KB, 下载次数: 1

跳转到指定楼层
tsl0617
发表于: 2010-1-27 12:33:51 | 显示全部楼层

快速回复 返回顶部 返回列表