刚刚发现程序有点问题,现在改进了一下,能找出正确答案了: 1,3,9,27
public class FindFama
{
int a ;
int b;
int c;
int d;
int num = 40;
int i = 0;
public FindFama()
{
long start = System.currentTimeMillis();
for(a = 0; a < num; a++)
for(b = 0; b < num - a ; b++)
for(c = 0; c < num - a - b; c++)
{
d = num - a - b - c;
i ++ ;
if (find(a, b, c, d))
{
System.out.print("a = " + a + ";");
System.out.print("b = " + b + ";");
System.out.print("c = " + c + ";");
System.out.println("d = " + d);
}
}
System.out.println("time cost " + (System.currentTimeMillis() - start));
System.out.println(i);
}
public boolean find(int a,int b, int c,int d)
{
for ( int weight = 1; weight < num + 1 ; weight++)
{
if( oneFama(a,b,c,d,weight)||twoFama(a,b,c,d,weight)
||threeFama(a,b,c,d,weight)||fourFama(a,b,c,d,weight)
){}
else
return false;
}
return true;
}
public boolean oneFama (int a, int b, int c, int d,int weight)
{
if( weight == a || weight == b || weight == c|| weight == d)
return true;
else
return false;
}
public boolean twoFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b || weight == a + c ||
weight == a + d || weight == b + c ||
weight == b + d ||
weight == c + d || weight + a == b ||
weight + a == c ||
weight + a == d || weight + b == a ||
weight + b == c || weight + b == d ||
weight + c == a || weight + c == b ||
weight + c == d || weight + d == a ||
weight + d == b || weight + d == c
)
return true;
else
return false;
}
public boolean threeFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c || weight == a + b + d ||
weight == b + c + d || weight == a + c + d ||
weight + a == b + c ||
weight + a == b + d || weight + a == c + d ||
weight + b == a + c || weight + b == a + d ||
weight + b == c + d || weight + c == a + b ||
weight + c == a + d || weight + c == b + d ||
weight + d == a + b || weight + d == a + c ||
weight + d == b + c || weight + b + c == a ||
weight + b + d == a || weight + c + d == a ||
weight + a + c == b || weight + a + d == b ||
weight + c + d == b || weight + a + b == c ||
weight + a + d == c || weight + b + d == c ||
weight + a + b == d || weight + a + c == d ||
weight + b + c == d )
return true;
else
return false;
}
public boolean fourFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c + d || weight + a == b + c + d ||
weight + b == a + c + d || weight + c == a + b + d ||
weight + b + c + d == a || weight + a + c + d == b ||
weight + a + b + d == c || weight + a + b + c == d ||
weight + d == a + b + c || weight + a + b == c + d ||
weight + c + d == a + b || weight + a + c == b + d ||
weight + b + d == a + c || weight + a + d == b + c ||
weight + b + c == a + d
)
return true;
else
return false;
}
public static void main(String[] args)
{
new FindFama();
不好意思,刚刚发错了
public class FindFama
{
int a ;
int b;
int c;
int d;
int num = 27;
public FindFama()
{
for(a = 0; a < num; a++)
for(b = 0; b < num; b++)
for(c = 0; c < num; c++)
{
d = num - a - b - c;
if(d > 0)
if(find(a,b,c,d))
{
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
else{}
else
break;
}
System.out.println("end");
}
public boolean find(int a,int b, int c,int d)
{
for ( int weight = 1; weight < num + 1 ; weight++)
{
if( oneFama(a,b,c,d,weight)||twoFama(a,b,c,d,weight)
||threeFama(a,b,c,d,weight)||fourFama(a,b,c,d,weight)
){}
else
return false;
}
return true;
}
public boolean oneFama (int a, int b, int c, int d,int weight)
{
if( weight == a || weight == b || weight == c|| weight == d)
return true;
else
return false;
}
public boolean twoFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b || weight == a + c ||
weight == a + d || weight == b + c ||
weight == b + d ||
weight == c + d || weight + a == b ||
weight + a == c ||
weight + a == d || weight + b == a ||
weight + b == c || weight + b == d ||
weight + c == a || weight + c == b ||
weight + c == d || weight + d == a ||
weight + d == b || weight + d == c
)
return true;
else
return false;
}
public boolean threeFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c || weight + a == b + c ||
weight + a == b + d || weight + a == c + d ||
weight + b == a + c || weight + b == a + d ||
weight + b == c + d || weight + c == a + b ||
weight + c == a + d || weight + c == b + d ||
weight + d == a + b || weight + d == a + c ||
weight + d == b + c || weight + b + c == a ||
weight + b + d == a || weight + c + d == a ||
weight + a + c == b || weight + a + d == b ||
weight + c + d == b || weight + a + b == c ||
weight + a + d == c || weight + b + d == c ||
weight + a + b == d || weight + a + c == d ||
weight + b + c == d )
return true;
else
return false;
}
public boolean fourFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c + d || weight + a == b + c + d ||
weight + b == a + c + d || weight + c == a + b + d ||
weight + b + c + d == a || weight + a + c + d == b ||
weight + a + b + d == c || weight + a + b + c == d ||
weight + d == a + b + c || weight + a + b == c + d ||
weight + c + d == a + b || weight + a + c == b + d ||
weight + b + d == a + c || weight + a + d == b + c ||
weight + b + c == a + d
)
return true;
else
return false;
}
public static void main(String[] args)
{
new FindFama();
楼主,你40克,不能找到答案吧,我的程序计算出超过27克就没有答案了
public class FindFama
{
int a ;
int b;
int c;
int d;
int num = 40;
public FindFama()
{
for(a = 0; a < num; a++)
for(b = 0; b < num; b++)
for(c = 0; c < num; c++)
{
d = num - a - b - c;
a = 1; b = 5; c = 16; d = 18;
if(d > 0)
if(find(a,b,c,d))
{
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
else{}
else
break;
}
System.out.println("end");
}
public boolean find(int a,int b, int c,int d)
{
for ( int weight = 1; weight < num + 1 ; weight++)
{
if( oneFama(a,b,c,d,weight)||twoFama(a,b,c,d,weight)
||threeFama(a,b,c,d,weight)||fourFama(a,b,c,d,weight)
){}
else
return false;
}
return true;
}
public boolean oneFama (int a, int b, int c, int d,int weight)
{
if( weight == a || weight == b || weight == c|| weight == d)
return true;
else
return false;
}
public boolean twoFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b || weight == a + c ||
weight == a + d || weight == b + c ||
weight == b + d ||
weight == c + d || weight + a == b ||
weight + a == c ||
weight + a == d || weight + b == a ||
weight + b == c || weight + b == d ||
weight + c == a || weight + c == b ||
weight + c == d || weight + d == a ||
weight + d == b || weight + d == c
)
return true;
else
return false;
}
public boolean threeFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c || weight + a == b + c ||
weight + a == b + d || weight + a == c + d ||
weight + b == a + c || weight + b == a + d ||
weight + b == c + d || weight + c == a + b ||
weight + c == a + d || weight + c == b + d ||
weight + d == a + b || weight + d == a + c ||
weight + d == b + c || weight + b + c == a ||
weight + b + d == a || weight + c + d == a ||
weight + a + c == b || weight + a + d == b ||
weight + c + d == b || weight + a + b == c ||
weight + a + d == c || weight + b + d == c ||
weight + a + b == d || weight + a + c == d ||
weight + b + c == d )
return true;
else
return false;
}
public boolean fourFama (int a, int b, int c, int d,int weight)
{
if( weight == a + b + c + d || weight + a == b + c + d ||
weight + b == a + c + d || weight + c == a + b + d ||
weight + b + c + d == a || weight + a + c + d == b ||
weight + a + b + d == c || weight + a + b + c == d ||
weight + d == a + b + c || weight + a + b == c + d ||
weight + c + d == a + b || weight + a + c == b + d ||
weight + b + d == a + c || weight + a + d == b + c ||
weight + b + c == a + d
)
return true;
else
return false;
}
public static void main(String[] args)
{
new FindFama();