Advertisement
Guest User

help me pls

a guest
Nov 19th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Scanner;
  8. import java.util.WeakHashMap;
  9.  
  10. public class KenKen {
  11.  
  12. public static void main(String[] args) throws FileNotFoundException {
  13. Scanner in = new Scanner(new File("KenKen.dat"));
  14.  
  15. while (in.hasNextLine()) {
  16. //String line = in.nextLine();
  17. Map<String, Integer> map = new WeakHashMap<String, Integer>();
  18. List<Integer> intsList = new ArrayList<Integer>() {
  19. /**
  20. *
  21. */
  22. private static final long serialVersionUID = 1L;
  23.  
  24. {
  25. add(1);
  26. add(2);
  27. add(3);
  28. add(4);
  29. add(5);
  30. add(6);
  31. }
  32. };
  33.  
  34. String line1 = in.nextLine();
  35. String line2 = in.nextLine();
  36. String line3 = in.nextLine();
  37.  
  38. System.out.println(line1 + " " + line2 + " " + line3);
  39. map.put(line1.split(" ")[0],
  40. Integer.parseInt(line1.split(" ")[1]));
  41. map.put(line2.split(" ")[0],
  42. Integer.parseInt(line2.split(" ")[1]));
  43. map.put(line3.split(" ")[0],
  44. Integer.parseInt(line3.split(" ")[1]));
  45. if (in.hasNextLine())
  46. in.nextLine();
  47. System.out.println("Size: " + map.size());
  48.  
  49. List<String> solutions = new ArrayList<String>();
  50. List<Integer> usedNums = new ArrayList<Integer>();
  51. for (Entry<String, Integer> entry : map.entrySet()) {
  52. System.out.println(entry.getKey() + " " + entry.getValue());
  53. innerloop: for (int i = 0; i < intsList.size(); i++) {
  54. int num1 = intsList.get(i);
  55. for (int j = 0; j < intsList.size(); j++) {
  56. int num2 = intsList.get(j);
  57. if (makesNum(num1, num2, entry.getValue(),
  58. entry.getKey())
  59. && !usedNums.contains(num1)
  60. && !usedNums.contains(num2)) {
  61. String s = num1 + " " + num2;
  62. solutions.add(s);
  63. System.out.println("Found solution for "
  64. + entry.getKey() + " " + entry.getValue()
  65. + ". It's " + num1 + " " + num2);
  66. usedNums.add(num1);
  67. usedNums.add(num2);
  68. break innerloop;
  69. }
  70. }
  71. }
  72. }
  73. if (solutions.size() == 3) {
  74. System.out.println("Possible.");
  75. } else {
  76. System.out.println("Not Possible.");
  77. }
  78. System.out.println("\n");
  79. }
  80. in.close();
  81. }
  82.  
  83. private static boolean makesNum(int i, int j, int num, String op) {
  84. if (op.equalsIgnoreCase("+"))
  85. return i + j == num;
  86. else if (op.equalsIgnoreCase("-"))
  87. return (i - j == num) || (j - i == num);
  88. else if (op.equalsIgnoreCase("*"))
  89. return i * j == num;
  90. else {
  91. return ((((double)i) / j) == (double) num) || ((((double)j) / i) == (double) num);
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement