讨论 / 24点有问题
wxm100wxm 2011-11-10 00:15:00
点我顶贴 收藏 删除
#include<iostream>

using namespace std;

int d[5];int r[4][5];

int i,result,n,len,x=0;

string p[5];

void print()

{int i,j;

for(i=1;i<=3;i++)

{

for(j=1;j<=3;j++)

if(j!=2)cout<<r[i][j];

else

{switch(r[i][j])

{case 1:{cout<<"+";break;}

case 2:{cout<<"-";break;}

case 3:{cout<<"*";break;}

case 4:{cout<<"/";break;}

}

}

cout<<"="<<r[i][4]<<endl;

}

}

void tryy(int k,int d[])

{int a,b,i,j,l,t;

int e[5];

if((k==1)&&(d[1]==24)){cout<<"yes"<<endl;x=1;//system("pause");

exit(0);}

else

{for(i=1;i<=k-1;i++)

for(j=i+1;j<=k;j++)

{a=d[i];b=d[j];

if(a<b){t=a;a=b;b=t;}

t=0;

for(l=1;l<=k;l++)

if((l!=i)&&(l!=j)){t++;e[t]=d[l];}

r[5-k][1]=a;r[5-k][3]=b;r[5-k][4]=-1;

for(l=1;l<=4;l++)

{

switch(l)

{case 1:{r[5-k][4]=a+b;break;}

case 2:{r[5-k][4]=a-b;break;}

case 3:{r[5-k][4]=a*b;break;}

case 4:if(b!=0)if(a%b==0)r[5-k][4]=a/b;

}

r[5-k][2]=l;

if(r[5-k][4]!=-1)

{e[t+1]=r[5-k][4];

tryy(k-1,e);

}

}

}

}

}

int main()

{for(i=1;i<=4;i++)

cin>>p[i];

for(i=1;i<=4;i++)

{

//cout<<p[i][0]<<" "<<int(p[i][0])<<endl;

if(p[i][0]=='A')

d[i+1]=1;

if(p[i][0]=='J')

d[i+1]=11;

if(p[i][0]=='Q')

d[i+1]=12;

if(p[i][0]=='K')

d[i+1]=13;

if((int(p[i][0])-48>=2)&&(int(p[i][0])-48<=9))

d[i+1]=int(p[i][0])-48;

if((int(p[i][0])-48==1))

{d[i+1]=10;}

}

tryy(4,d);

if(x==0)

cout<<"no"<<endl;

//system("pause");

return 0;

}

这个程序60分,求解。到底什么原因??????

#1 wh3z_wsc@2011-11-09 03:42:00
回复 删除
BC如下

#include <cstdio>

#include <cstring>

typedef char String[20];

int a[4], b[4], path[4][2];

String ans[4], buf, Min = "zzzzz";

char op[4], Data[4];

void outit() {

int i;

for (i = 0; i < 4; i ++)

sprintf(ans[i], "%c", Data[i]);

for (i = 0; i < 3; i ++) {

sprintf(buf, "(%s%c%s)", ans[path[i][0]], op[i], ans[path[i][1]]);

strcpy(ans[path[i][0]], buf);

}

if (strcmp(Min, ans[path[2][0]]) > 0)

strcpy(Min, ans[path[2][0]]);

}

void dfs(int l) {

if (l == 3) {

int i;

for (i = 0; i < 4; i ++)

if (a[i] != 0) break;

if (i < 4 && a[i] == 24) outit();

return;

}

for (int i = 0; i < 4; i ++)

for (int j = 0; j < 4; j ++)

if (i != j && a[i] != 0 && a[j] != 0) {

int t1, t2;

t1 = a[i]; t2 = a[j];

a[i] = a[i] + a[j]; a[j] = 0;

path[l][0] = i; path[l][1] = j; op[l] = '+';

dfs(l + 1);

a[i] = t1; a[j] = t2;

t1 = a[i]; t2 = a[j];

a[i] = a[i] - a[j]; a[j] = 0;

path[l][0] = i; path[l][1] = j; op[l] = '-';

dfs(l + 1);

a[i] = t1; a[j] = t2;

t1 = a[i]; t2 = a[j];

a[i] = a[i] * a[j]; a[j] = 0;

path[l][0] = i; path[l][1] = j; op[l] = '*';

dfs(l + 1);

a[i] = t1; a[j] = t2;

t1 = a[i]; t2 = a[j];

a[i] = a[i] & a[j]; a[j] = 0;

path[l][0] = i; path[l][1] = j; op[l] = '&';

dfs(l + 1);

a[i] = t1; a[j] = t2;

t1 = a[i]; t2 = a[j];

a[i] = a[i] | a[j]; a[j] = 0;

path[l][0] = i; path[l][1] = j; op[l] = '|';

dfs(l + 1);

a[i] = t1; a[j] = t2;

t1 = a[i]; t2 = a[j];

a[i] = a[i] ^ a[j]; a[j] = 0;

path[l][0] = i; path[l][1] = j; op[l] = '^';

dfs(l + 1);

a[i] = t1; a[j] = t2;

if (a[i] % a[j] == 0) {

t1 = a[i]; t2 = a[j];

a[i] = a[i] / a[j]; a[j] = 0;

path[l][0] = i; path[l][1] = j; op[l] = '/';

dfs(l + 1);

a[i] = t1; a[j] = t2;

}

}

}

int main() {

char ch;

freopen("point24.in", "r", stdin);

freopen("point24.out", "w", stdout);

memset(path, -1, sizeof(path));

for (int i = 0; i < 4; i ++) {

scanf("%c ", &ch);

Data[i] = ch;

if (ch == 'A') a[i] = 1;

else if (ch == 'T') a[i] = 10;

else if (ch == 'J') a[i] = 11;

else if (ch == 'Q') a[i] = 12;

else if (ch == 'K') a[i] = 13;

else a[i] = ch - '0';

}

memcpy(b, a, sizeof(a));

dfs(0);

if (Min[0] == 'z') printf("Poor!\n");

else printf("%s\n", Min);

return 0;

}

#2 87933801@2011-11-09 23:59:00
回复 删除
西
#3 吴钰晗@2011-11-10 00:02:00
回复 删除
我什么都不知道!!!真的!!!!谁教我?????????
#4 吴钰晗@2011-11-10 00:02:00
回复 删除
我什么都不知道!!!真的!!!!谁教我?????????
#5 87933801@2011-11-10 00:02:00
回复 删除
[quote][url=/Redirect.asp?Act=Reply&DID=10164&RID=23808]原帖[/url]由 [i]吴钰晗[/i] 于 2011-11-10 16:02:00 发表

我什么都不知道!!!真的!!!!谁教我?????????[/quote]

#6 吴钰晗@2011-11-10 00:02:00
回复 删除
[quote][url=/Redirect.asp?Act=Reply&DID=10164&RID=23810]原帖[/url]由 [i]87933801[/i] 于 2011-11-10 16:02:00 发表

[/quote]

#7 87933801@2011-11-10 00:04:00
回复 删除
把print删掉
#8 87933801@2011-11-10 00:04:00
回复 删除
删掉a,b,改成di,dj
#9 87933801@2011-11-10 00:05:00
回复 删除
[quote][url=/Redirect.asp?Act=Reply&DID=10164&RID=23811]原帖[/url]由 [i]吴钰晗[/i] 于 2011-11-10 16:02:00 发表

[/quote]

拉丁美洲

#10 wxm100wxm@2011-11-10 00:15:00
回复 删除
xiexie
查看更多回复
提交回复