欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

洛谷P1618 Java解法

程序员文章站 2022-07-16 12:22:46
...
题目出处:https://www.luogu.com.cn/problem/P1618

洛谷P1618 Java解法

思路:
从结果入手
先在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;
		// 123~987
		/*
		 * 从a开始遍历:不包括零,不能有重复的数字
		 * 
		 */
		for (a = 123; a <= 987; a++) {
			// a肯定能被A整除
			if (a % A == 0) {
				// 不包含零且没有数字重复
				if (!isContZero(a) && !isRepeated(a)) {
					// 求出b,因为是int型,所以肯定是整数
					b = a / A * B;
					if (isBetween(b) && !isContZero(b) && !isRepeated(b)) {
						c = a / A * C;
						if (isBetween(c) && !isContZero(c) && !isRepeated(c)) {
							// 判断a,b,c各个数字是否重复,如果没有重复
							if (!isAllUsed(a, b, c)) {
								//先将isFound置为true说明找到
								isFound = true;
								System.out.printf("%d %d %d",a,b,c);
								System.out.println();
							}
						}
					}
				}
			}
		}
		
		//循环完毕
		//没找到才输出
		if (!isFound) {
			System.out.println("No!!!");
		}
	}

	// 判断是否包含零
	// 包含返回true,不包含返回false
	public static boolean isContZero(int beJudged) {
		return String.valueOf(beJudged).contains("0");
	}

	// 判断是否有数字重复
	// 包含返回true,不包含返回false
	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;
	}

	// 判断是否在123~987之间
	// 包含返回true,不包含返回false
	public static boolean isBetween(int beJudged) {
		if (beJudged >= 123 && beJudged <= 987) {
			return true;
		}

		return false;
	}

	// 判断三个数字的各个位数是否有相等的
	// 有则返回true,没有则返回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);

		// a的各位数,
		int age = aArr[0];
		int ashi = aArr[1];
		int abai = aArr[2];
		// b的各位数
		int bge = bArr[0];
		int bshi = bArr[1];
		int bbai = bArr[2];
		// c的各位数
		int cge = cArr[0];
		int cshi = cArr[1];
		int cbai = cArr[2];

		// 先将a相应位置记为true
		isUsed[age-1] = true;
		isUsed[ashi-1] = true;
		isUsed[abai-1] = true;

		// 当b的各位数对应数字都没被使用过时
		if (isUsed[bge-1] == false && isUsed[bshi-1] == false && isUsed[bbai-1] == false) {
			// 如果b对应数字都没被访问过,先将b个数字对应位置置为true
			isUsed[bge-1] = true;
			isUsed[bshi-1] = true;
			isUsed[bbai-1] = true;
			// 再判断c,当c的各位数对应数字都没被使用过时
			if (isUsed[cge-1] == false && isUsed[cshi-1] == false && isUsed[cbai-1] == false) {
				//说明a,b,c满足条件
				return false;
			} else {// 说明c有数字被使用过,返回true
				return true;
			}
		} else {// 说明b有数字被使用过,返回true
			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;
	}
}

相关标签: 洛谷