庞果网上看到的
然后自己写了一个,可是有点问题,有的时候正常有的时候死循环求教啊
import java.util.*;
public class Lianxi1
{
public static void main(String[] arge)
{
int a,b,c;
a=(int)(Math.random()*1000000);
System.out.print("生成A为:"+a);
b=(int)(Math.random()*1000000);
System.out.print("生成B为:"+b);
c=(int)(Math.random()*1000000);
System.out.print("生成C为:"+c);
System.out.println("水缸是否刚好有C升水?");
System.out.println(a+b+c);//测试程序进行
boolean bl=Lianxi1.can(a,b,c); //判断是否能成功
System.out.println(bl);
}
public static boolean can(int a,int b,int c)
{
int sum=0;
int d=0;
int difference=0;
int c2=0;
if(a<b)
{
d=a;
a=b;
b=d;
}
difference=a-b;
if(b%difference!=0||c%difference==0){
while(sum!=c)
{
if(difference<c-sum)
{
sum+=difference;
}else{
int b2=0,a2=0;
while(difference!=c-sum)
{
c2=difference;
b2=b-c2;
a2=a-b2 ;
difference=a2-b;
}
sum+=difference;
}
}
return true;
}else{ return false;}
}
}
相关解决方案