Java实现 洛谷 P1618 三连击(升级版)

在这里插入图片描述

import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
	private static Scanner cin;
	private static boolean hasFound = false; 
	
	public static void main(String args[]) throws Exception {
		cin = new Scanner(System.in);
		int A = cin.nextInt();
		int B = cin.nextInt();
		int C = cin.nextInt();
		
		for(int i = 123;i<=987;i++) {
			if(i%A==0) {
				if(String.valueOf(i).contains("0")) {
					continue;
				}
				int j = i/A*B;
				if(j<123 || j>987 || String.valueOf(j).contains("0")) {
					continue;
				}else {
					int k = i/A*C;
					if(k<123 || k>987 || String.valueOf(k).contains("0")) {
						continue;
					}else {
						checkDigit(i,j,k);
					}
				}
			}
		}
		if(!hasFound) {
			System.out.println("No!!!");
		}
				
	}
	
	//check if i,j,k has different digits from 1 to 9
	public static void checkDigit(int i,int j,int k) {
		int[] digits = new int[9];
		Arrays.fill(digits, 0);
		int a = i/100;
		int b = (i-a*100)/10;
		int c = (i-a*100 - b*10);
		if(a == b || b== c || a==c) {
			return;
		}
		if(digits[a-1] ==1 || digits[b-1] == 1 || digits[c-1] == 1) {
			return;
		}else {
			digits[a-1] = 1;
			digits[b-1] = 1;
			digits[c-1] = 1;
			a = j/100;
			b = (j-a*100)/10;
			c = (j-a*100 - b*10);
			if(a == b || b== c || a==c) {
				return;
			}
			if(digits[a-1] ==1 || digits[b-1] == 1 || digits[c-1] == 1) {
				return;
			}else {
				digits[a-1] = 1;
				digits[b-1] = 1;
				digits[c-1] = 1;
				a = k/100;
				b = (k-a*100)/10;
				c = (k-a*100 - b*10);
				if(a == b || b== c || a==c) {
					return;
				}
				if(digits[a-1] ==1 || digits[b-1] == 1 || digits[c-1] == 1) {
					return;
				}else {
					System.out.println(String.format("%d %d %d", i,j,k));
					if(!hasFound) {
						hasFound = true;
					}
				}
			}
		}
	}
}
posted @ 2019-07-30 18:31  南墙1  阅读(85)  评论(0编辑  收藏  举报