拼火柴

小明帮妹妹辅导寒假作业,其中一道题是利用火柴棒拼等式,题目给你20根火柴棍,问可以拼出多少个形如 "A+B=C" 的等式?
注意:
数字0-9分别需要的火柴棒的数目为: 6,2,5,5,4,5,6,3,7,6 
等式中的 A、B、C 是用火柴棍拼出的整数
若A、B、C 不为0,则最高位不能为0
加号与等号各自需要两根火柴棍
如果 A!=B,则 A+B=C与 B+A=C 视为不同的等式(A,B,C>=0)
n 根火柴棍必须全部用上
答案是一个整数,请通过浏览器直接提交该数字。

暴力枚举 简单模拟

39

public class C {
    static int a[]={6,2,5,5,4,5,6,3,7,6};
	public static void main(String[] args) {
		// TODO Auto-generated method stub
         int count=0,dou=0;
         for(int i=0;i<300;i++)
        	 for(int j=0;j<300;j++)
        		  for(int k=0;k<600;k++)
        		  {
        			  if(i+j==k)
        			  {
        				  int x=div(i);
        				  int y=div(j);
        				  int z=div(k);
        				  if(x+y+z==16)
        				  {
        					  count++;
        					  System.out.println(i+" "+j+" "+k);
        				  }
        			  }
        		  }
         System.out.println(count);
         
	}
	private static int div(int i) { //将多位数的每一位 拆分成多个 个位数
		// TODO Auto-generated method stub
		int ans=0;
		if(i>=100)
		{
			ans=ans+a[i/100];
			i=i%100;
		    ans=ans+a[i/10];
		    i=i%10;
		    ans=ans+a[i];
		 }
		else if(i>=10)
		{  ans=ans+a[i/10];
	       i=i%10;
	       ans=ans+a[i];
		}
		else
			ans=ans+a[i];
		return ans;
	}

}

猜你喜欢

转载自blog.csdn.net/qq_42167059/article/details/86841344