题目出处:https://www.luogu.com.cn/problem/P1618
思路:
从结果入手
先在123—987中找到满足A:B:C的三个数
再判断三个数的各个数字是否有重复的
package violence;
import java.util.Scanner;
public class P1618 {
static Scanner sc;
static boolean isFound = false;
static int A;
static int B;
static int C;
public static void main(String[] args) {
sc = new Scanner(System.in);
A = sc.nextInt();
B = sc.nextInt();
C = sc.nextInt();
int a, b, c;
for (a = 123; a <= 987; a++) {
if (a % A == 0) {
if (!isContZero(a) && !isRepeated(a)) {
b = a / A * B;
if (isBetween(b) && !isContZero(b) && !isRepeated(b)) {
c = a / A * C;
if (isBetween(c) && !isContZero(c) && !isRepeated(c)) {
if (!isAllUsed(a, b, c)) {
isFound = true;
System.out.printf("%d %d %d",a,b,c);
System.out.println();
}
}
}
}
}
}
if (!isFound) {
System.out.println("No!!!");
}
}
public static boolean isContZero(int beJudged) {
return String.valueOf(beJudged).contains("0");
}
public static boolean isRepeated(int beJudged) {
int bai = beJudged / 100;
int shi = (beJudged - bai * 100) / 10;
int ge = (beJudged - bai * 100 - shi * 10);
if (bai == shi || shi == ge || ge == bai) {
return true;
}
return false;
}
public static boolean isBetween(int beJudged) {
if (beJudged >= 123 && beJudged <= 987) {
return true;
}
return false;
}
public static boolean isAllUsed(int a, int b, int c) {
boolean[] isUsed = new boolean[9];
int aArr[] = GSB(a);
int bArr[] = GSB(b);
int cArr[] = GSB(c);
int age = aArr[0];
int ashi = aArr[1];
int abai = aArr[2];
int bge = bArr[0];
int bshi = bArr[1];
int bbai = bArr[2];
int cge = cArr[0];
int cshi = cArr[1];
int cbai = cArr[2];
isUsed[age-1] = true;
isUsed[ashi-1] = true;
isUsed[abai-1] = true;
if (isUsed[bge-1] == false && isUsed[bshi-1] == false && isUsed[bbai-1] == false) {
isUsed[bge-1] = true;
isUsed[bshi-1] = true;
isUsed[bbai-1] = true;
if (isUsed[cge-1] == false && isUsed[cshi-1] == false && isUsed[cbai-1] == false) {
return false;
} else {
return true;
}
} else {
return true;
}
}
public static int[] GSB(int beJudged) {
int bai = beJudged / 100;
int shi = (beJudged - bai * 100) / 10;
int ge = (beJudged - bai * 100 - shi * 10);
int arr[] = { ge, shi, bai };
return arr;
}
}