Fork me on GitHub

马虎的算式


本人解法 比较笨

public class Main {
	// 测试代码
	public static void main(String[] args) {
		// 定义一个字符串用于连接五个数字
		int sum = 0;
		for (int i = 10; i <= 99; i++) {
			for (int x = 100; x <= 999; x++) {
				String str = "";
				Boolean flag = true;
				str = str + i;
				str = str + x;
				// 用于记录每个字母出现的次数
				int[] arr = new int[10];
				for (int y = 0; y < 5; y++) {
					String s = String.valueOf(str.charAt(y));
					int value = Integer.parseInt(s);
					arr[value]++;
					if (arr[value] == 2 ||value ==0) {
						flag = false;
						break;
					}
				}
				if (flag == true) {
					String s1 = "";
					String s2 = "";
					s1 = s1 + String.valueOf(str.charAt(0));
					s1 = s1 + String.valueOf(str.charAt(3));
					s1 = s1 + String.valueOf(str.charAt(1));
					s2 = s2 + String.valueOf(str.charAt(2));
					s2 = s2 + String.valueOf(str.charAt(4));
					int s3 = Integer.parseInt(s1);
					int s4 = Integer.parseInt(s2);
					if ((s3 * s4) == (i * x)) {
						sum++;
					}
				}
			}
		}
		System.out.println(sum);
	}
}

另一种解法

public static void main(String[] args) {
		int sum = 0;
		for (int a = 1; a <= 9; a++) {
			for (int b = 1; b <= 9; b++) {
				if (b != a) {
					for (int c = 1; c <= 9; c++) {
						if (c != b && c != a) {
							for (int d = 1; d <= 9; d++) {
								if (d != c && d != b && d != a) {
									for (int e = 1; e <= 9; e++) {
										if (e != d && e != c && e != b && e != a) {
											if ((a * 10 + b) * (c * 100 + d * 10 + e) == (a * 100 + d * 10 + b)* (c * 10 + e)) {
												sum++;
												System.out.println(sum);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
posted @ 2019-07-08 13:22  cznczai  阅读(610)  评论(0编辑  收藏  举报