#include <string.h>
int c24
char expr3[80]
char expr4[80]
void count2( int a, int b )
{
if ( a + b == 24 )
{
c24 ++
printf( "%s\t%s\t%d+%d=24\n", expr4, expr3, a, b )
}
if ( a * b == 24 )
{
c24 ++
printf( "%s\t%s\t%d+%d=24\n", expr4, expr3, a, b )
}
if ( a * b == 24 )
{
c24 ++
printf( "%s\t%s\t%d*%d=24\n", expr4, expr3, a, b )
}
if ( a - b == 24 )
{
c24 ++
printf( "%s\t%s\t%d-%d=24\n", expr4, expr3, a, b )
}
if ( b - a == 24 )
{
c24 ++
printf( "%s\t%s\t%d-%d=24\n", expr4, expr3, b, a )
}
if ( (b!=0) &&(a==24*b) )
{
c24 ++
printf( "%s\t%s\t%d/%d=24\n", expr4, expr3, a, b )
}
if ( (a!=0) &&(b==24*a) )
{
c24 ++
printf( "%s\t%s\t%d/%d=24\n", expr4, expr3, b, a )
}
}
void count3( int a, int b, int c )
{
int dd[3]
int i,j,k,nd
dd[0] = a
dd[1] = b
dd[2] = c
for ( i = 0i <3i++ )
for ( j = i+1j <3j++ )
{
k = 3 - i - j
nd = dd[i] + dd[j]
sprintf( expr3, "%d+%d=%d", dd[i], dd[j], nd )
count2( nd, dd[k] )
nd = dd[i] * dd[j]
sprintf( expr3, "%d*%d=%d", dd[i], dd[j], nd )
count2( nd, dd[k] )
if ( dd[i]>dd[j] )
{
nd = dd[i] - dd[j]
sprintf( expr3, "%d-%d=%d", dd[i], dd[j], nd )
}
else
{
nd = dd[j] - dd[i]
sprintf( expr3, "%d-%d=%d", dd[j], dd[i], nd )
}
count2( nd, dd[k] )
if ( (dd[j]!=0) &&(dd[i]%dd[j]==0) )
{
nd = dd[i] / dd[j]
sprintf( expr3, "%d/%d=%d", dd[i], dd[j], nd )
count2( nd, dd[k] )
}
else if ( (dd[i]!=0) &&(dd[j]%dd[i]==0) )
{
nd = dd[j] / dd[i]
sprintf( expr3, "%d/%d=%d", dd[j], dd[i], nd )
count2( nd, dd[k] )
}
}
}
void count4( int a, int b, int c, int d )
{
int dd[4]
int i,j,k,m,nddd[0] = a
dd[1] = b
dd[2] = c
dd[3] = d
for ( i = 0i <4i++ )
for ( j = i+1j <4j++ )
{
for ( k = 0(k==i) || (k==j)k++ )
m = 6 - i - j - k
nd = dd[i] + dd[j]
sprintf( expr4, "%d+%d=%d", dd[i], dd[j], nd )
count3( nd, dd[k], dd[m] )
nd = dd[i] * dd[j]
sprintf( expr4, "%d*%d=%d", dd[i], dd[j], nd )
count3( nd, dd[k], dd[m] )
if ( dd[i]>dd[j] )
{
nd = dd[i] - dd[j]
sprintf( expr4, "%d-%d=%d", dd[i], dd[j], nd )
}
else
{
nd = dd[j] - dd[i]
sprintf( expr4, "%d-%d=%d", dd[j], dd[i], nd )
}
count3( nd, dd[k], dd[m] )
if ( (dd[j]!=0) &&(dd[i]%dd[j]==0) )
{
nd = dd[j] / dd[i]
sprintf( expr4, "%d/%d=%d", dd[j], dd[i], nd )
count3( nd, dd[k], dd[m] )
}
}
}
main()
{
int a,b,c,d
printf( "Input 4 Number for Count 24: " )
scanf( "%d%d%d%d", &a, &b, &c, &d )
c24 = 0
count4( a, b, c, d )
if ( c24==0 )
printf( "=======failed======\n" )/***/
printf("End of C Program, Press Enter.\n")
getchar()
getchar()
return 0
}
#include <stdio.h>#include <stdlib.h>
#include <math.h>
char op[3], o[5]="+-*/"
float n[4], on[10]
int used[4] = {0}, top=0, tp=0, x
void chk(float k)
void search24(int d)
float calc(float n1, float n2, char o)
void make(int i, float p, float q, char o, int d)
int main( void )
{
printf("please input 4 card number:\n")
scanf("%f%f%f%f", &n[0], &n[1], &n[2], &n[3])
search24(0)
printf("No answer.\n")
return 0
}
void chk(float k)
{
if( (tp != 3) || ( fabs(k-24.0) >0.000001 )) //没有用完3个运算符或者结果不为24就退出.
return
for(x=0x<5x+=2) //这样设计是为了使3个选中的符号都可以得到输出.
printf("%g%c%g=%g\n", on[x], op[x/2], on[x+1], //分析得到的.
calc(on[x], on[x+1], op[x/2]))
system("pause")
exit(0)
}
float calc(float n1, float n2, char o)
{
switch(o){
case '+': return (n1+n2)
case '-': return (n1-n2)
case '*': return (n1*n2)
case '/': return (n1/n2)
default: exit(0)
}
}
void make(int i, float p, float q, char o, int d)
{
if(fabs(q)>0.000001 || o!='/') //除数不为0,或者为0的时候不能为除数.
n[i] = calc(p, q, o)
op[tp++] = o
chk(n[i])
search24(d+1)
tp-- //因为是全是全局变量,所以在做试验性的循环递归问题时,如果失败,要在递归函数后面重新恢复回原来的值
}
void search24(int d)
{
int i, j, k
float p, q
if(d>=3) //控制递归深度,就是运算符的输出个数.
return
for(i=0i<4i++)
for(j=0j<4j++)
if( (i!=j)&&(used[i]+used[j] == 0) ) //i!=j是防止重复,(used[i]+used[j] == 0)是防止又再匹配已经用过的j,
//但是i可以新来.
{
used[j] = 1 //j得到匹配之后,赋值为1,表示已经使用
p=n[i]
q=n[j]
on[top++] = p
on[top++] = q
for(k=0k<4k++) //运算符的循环试用.
make(i, p, q, o[k], d)
n[i] = p //因为是全是全局变量,所以在做试验性的循环递归问题时,
used[j] = 0 //如果失败,要在递归函数后面重新恢复回原来的值
top -= 2 //
}
}
出处:http://blog.sina.com.cn/s/blog_491de9d60100d5er.html
n=n*i改为
i=i*n
因为你的n才是循环的条件,在这里面的n是不断变化的,这个循环应该是转不到头的
如果你的意思并非如此,那你需要再设定一个变量来替代这个n
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)