Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A mind teaser: fill the digits once #126

Open
FredWe opened this issue Mar 1, 2017 · 0 comments
Open

A mind teaser: fill the digits once #126

FredWe opened this issue Mar 1, 2017 · 0 comments

Comments

@FredWe
Copy link
Owner

FredWe commented Mar 1, 2017

image

make it equal by filling each digit [1 ... 9] once in the block:
      a b
*       c
----------
      d e
+     f g
----------
=     h i
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
		helper(nums, new ArrayList());
        return;
    }
    public static void helper(int[] nums, List<Integer> tempPerm) {
    	if (nums.length == tempPerm.size()) {
    		int ab = tempPerm.get(0) * 10 + tempPerm.get(1);
    		int c = tempPerm.get(2);
    		int de = tempPerm.get(3) * 10 + tempPerm.get(4);
    		int fg = tempPerm.get(5) * 10 + tempPerm.get(6);
    		int hi = tempPerm.get(7) * 10 + tempPerm.get(8);
    		if (ab * c == de && de + fg == hi) System.out.println(tempPerm);
    	}
        for (int n: nums) {
        	if (tempPerm.contains(n)) continue;
        	tempPerm.add(n);
        	helper(nums, tempPerm);
        	tempPerm.remove(tempPerm.size() - 1);
        }
    }
}
>>  [1, 7, 4, 6, 8, 2, 5, 9, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant