1259: [蓝桥杯2015初赛]三羊献瑞-java

1259: [蓝桥杯2015初赛]三羊献瑞-java_第1张图片

 暴力解法:

public class t1259 {
    public static void main(String[] args) {
        for (int a = 1; a <=9 ; a++) {
            for (int b = 0; b <=9 ; b++) {
                if (a==b){continue;}
                for (int c = 0; c <=9 ; c++) {
                    if (c==a||c==b){continue;}
                    for (int d = 0; d <=9 ; d++) {
                        if (d==a || d==b || d==c){continue;}
                        for (int e = 1; e <=9 ; e++) {
                            if (e==a || e==b || e==c || e==d ){continue;}
                            for (int f = 0; f <=9 ; f++) {
                                if (f==a || f==b || f==c || f==d|| f==e){continue;}
                                for (int g = 0; g <=9 ; g++) {
                                    if (g==a||g==b||g==c||g==d||g==e||g==f){continue;}
                                    for (int h = 0; h <=9 ; h++) {
                                        if (h==a||h==b||h==c||h==d||h==e||h==f||h==g){continue;}
                                        int num1 = a*1000+b*100+c*10+d;
                                        int num2 = e*1000+f*100+g*10+b;
                                        int sum = num1+num2;
                                        int t1 = sum%10;
                                        sum = sum/10;
                                        int t2 = sum%10;
                                        sum = sum/10;
                                        int t3 = sum%10;
                                        sum = sum/10;
                                        int t4 = sum%10;
                                        int t5 = sum/10;
                                      if (t5==e && t4==f && t3==c && t2==b && t1==h){
                                          System.out.println(num2);
                                      }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

 简便方法:


public class t1259 {
public static void main(String [] args) {
	/*
	 a b c d 
+	 e f g b
-------------
   e f c b i
   e=1 a=9 f=0 c=b+1 c+g>10 d b g
	 */
	
	for(int d=2;d<9;d++) {
		for(int b =2;b<9 ;b++) {
			if(b==d)continue;
			for(int g=2;g<9;g++) {
				if(g==b || g==d)continue;
				int c = b+1;
				if(c==d || c==g || c==9)continue;
				
				for(int i=2;i<9;i++) {
					if(i==d||i==b||i==g||i==c)continue;
					int sum = 9000+b*100+c*10+d + 1000+g*10+b;
					if(sum==10000+c*100+b*10+i) {
						System.out.print(1000+g*10+b);
					}
				}
				
				
			}
		}
	}
}
}

答案:1259: [蓝桥杯2015初赛]三羊献瑞-java_第2张图片

你可能感兴趣的:(蓝桥杯,java,职场和发展)