7-3凑算式Java、暴力

凑算式

在这里插入图片描述
这个算式中A-I代表1-9的数字,不同的字母代表不同的数字。

比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。

这个算式一共有多少种解法?29

解题思路:直接for循环即可,最坑的是,数据类型不小心就使用了int,这就错了。比如:4/3=1, 5/3=1,很明显这是不可以的,所以数据类型可以使用float或者double。
public class Demo_7_03 {

	public static void main(String[] args) {
		int count=0;
		for(double a=1;a<10;a++) {
			for(double b=1;b<10;b++) {
				if(a==b)continue;
				for(double c=1;c<10;c++) {
					if(a==c||b==c)continue;
					for(double d=1;d<10;d++) {
						if(a==d||b==d||c==d)continue;
						for(double e=1;e<10;e++) {
							if(a==e||b==e||c==e||d==e)continue;
							for(double f=1;f<10;f++) {
								if(a==f||b==f||c==f||d==f||e==f)continue;
								for(double g=1;g<10;g++) {
									if(a==g||b==g||c==g||d==g||e==g||f==g)continue;
									for(double h=1;h<10;h++) {
										if(a==h||b==h||c==h||d==h||e==h||f==h||g==h)continue;
										for(double i=1;i<10;i++) {
											if(a==b||a==c||a==d||a==e||a==f||a==g||a==h||a==i||b==c||b==d||b==e||b==f||b==g||b==h||b==i||c==d||c==e||c==f||c==g||c==h||c==i||d==e||d==f||d==g||d==h||d==i||e==f||e==g||e==h||e==i||f==g||f==h||f==i||g==h||g==i||h==i){
												continue;
											}
											else {
												double m=d*100+e*10+f;
												double n=g*100+h*10+i;
												if((a+(b/c)+(m/n))==10) {
													count++;
												}
											}										
										}
									}
								}
							}
						}
					}
				}
			}
		}
		System.out.println(count);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41765969/article/details/88668684
7-3