蓝桥杯2017模拟赛-猜算式

蓝桥杯2017模拟赛-猜算式_第1张图片

直接爆了枚举下就好

//答案179 224 716 358 358 40096
public class Main2 {
    public static void main(String[] args) {
        for(int i = 100;i<=999;i++){
            for(int j = 100;j<=999;j++){
                int[] f = new int[10];
                int a = i*(j%10);
                int b = i*(j/10%10);
                int c = i*(j/100);
                if(a>=1000||b>=1000||c>=1000||i*j>=100000||a<100||b<100||c<100)continue;
                //i,j,a,b,c,i*j
                if(check(i,f)&&check(j,f)&&check(a,f)&&check(b,f)&&check(c,f)&&check(i*j,f)){
                    System.out.println(i+" "+j+" "+a+" "+b+" "+c+" "+i*j);
                }
            }
        }
    }
    public static boolean check(int i,int[] f){

        while(i>0){
            if(f[i%10]==2){
                return false;
            }
            f[i%10]++;
            i/=10;
        }
        return true;
    }
}

因为有人要求写下C的,所以我就顺便贴出C的代码,方便查看

代码:

    #include
#include
#include
int check(int i,int * f){
    while(i>0){
        if(f[i%10]==2){
            return 0;
        }
        f[i%10]++;
        i/=10;
    }
    return 1;

}
int main(){
    for(int i = 100;i<=999;i++){
        for(int j = 100;j<=999;j++){
            int f[10] = {0};
            int a = i*(j%10);
            int b = i*(j/10%10);
            int c = i*(j/100);
            if(a>=1000||b>=1000||c>=1000||i*j>=100000||a<100||b<100||c<100)continue;
            //i,j,a,b,c,i*j
            if(check(i,f)&&check(j,f)&&check(a,f)&&check(b,f)&&check(c,f)&&check(i*j,f)){
                printf("%d %d %d %d %d %d",i,j,a,b,c,i*j);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(蓝桥杯2017模拟赛)