您的位置:首页 > 其它

蓝桥杯2017模拟赛-猜算式

2017-03-16 13:55 183 查看


直接爆了枚举下就好

//答案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<stdio.h>
#include<math.h>
#include<stdlib.h>
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: