3.凑算式


这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?

注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。


方法一:暴力破解法  答案:29
public class _javaB03 {
	public static void main(String args[])  
    {  
        double a,b,c,count=0;        
        for(int A=1;A<=9;A++)  
        {                
            a=A;  
            for(int B=1;B<=9;B++)  
            {  
                for(int C=1;C<=9;C++)  
                {  //这是关键的容易出错的一步,因为如果b是int类型,会出现5/4=1而不是1.25会导致后面出错  
                    b=B*1.00/C;    
                    for(int D=1;D<=9;D++)  
                    {  
                        for(int E=1;E<=9;E++)  
                        {  
                            for(int F=1;F<=9;F++)  
                            {  
                                for(int G=1;G<=9;G++)  
                                {  
                                    for(int H=1;H<=9;H++)  
                                    {  
                                        for(int I=1;I<=9;I++)  
                                        {  
                                            c=(D*100+E*10+F)*1.00/(G*100+H*10+I);  
                                            //用来检验是否9个数有重复
                                            int []k=new int[10];    
                                            k[A]++;k[B]++;k[C]++;k[D]++;k[E]++;  
                                            k[F]++;k[G]++;k[H]++;k[I]++;  
                                            int i;  
                                            for(i=1;i<=9;i++)  
                                            {  
                                                //如果有一个数重复则无效
                                            	if(k[i]!=1)break; 
                                                //只有当所有数都为1,且满足和为是10才计数 
                                            	if(i==9&&a+b+c==10)count++; 
                                            }                                                                                                                                    
                                        }  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
        }  
        System.out.println(count);  
    }  
}
b=B*1.00/C;
看不懂*1.00的用法是什么,删掉以后结果为很大的错误值

方法二:

public class 凑算式 {
	static int count = 0;
	public static void main(String[] args) {
		float a[] = new float[10];
		boolean visit[] = new boolean[10];
		dfs(a,visit,1);
		System.out.println(count);
	}
	private static void dfs(float[] a, boolean[] visit, int num) {
		if (num==10) {
			if (judge(a)) {
				count++;
			}
			return;
		}		
		for (a[num] = 1; a[num] < 10; a[num]++) {
			if (visit[(int) a[num]]==false) {
				visit[(int) a[num]]=true;
				num = num + 1;
				dfs(a, visit, num);
				num = num - 1;
				visit[(int) a[num]] = false;
			}
		}
	}
	private static boolean judge(float[] a) {
		float A = a[1];
		float B = a[2]/a[3];
		float C = a[4]*100+a[5]*10+a[6];
		float D = a[7]*100+a[8]*10+a[9];
		if (A+B+C/D==10) {
			return true;
		}
		return false;
	}
}
 static boolean k[]=new boolean[10];  //判断这个数有没有被取过  
 static int g[]=new int[10];//存放9个数  
 static int count=0;  
 public static void main(String args[])  
 {  
      ff(1);  
      System.out.println(count);  
 }  
 public static void ff(int m)  
 {  
     if(m==10){  
        if(check())count++;  
     }  
     for(int i=1;i<=9;i++)  
     {  
         if(k[i]==false)  
         {  
             k[i]=true;    //回溯法  
             g[m]=i;  
             ff(m+1);  
             k[i]=false;  
         }  
     }  
 }  
 public static boolean check()  //检验是不是为10  
 {  
     double q=g[1];  
     double w=g[2]*1.00/g[3];  
     double e=(g[4]*100+g[5]*10+g[6])*1.00/(g[7]*100+g[8]*10+g[9]);  
     if(q+w+e==10.00)return true;  
     else return false;  
 }  
}  



猜你喜欢

转载自blog.csdn.net/qq_36227329/article/details/79644009