Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. Outputs all possibilities to put + or - or nothing between the numbers 1,2,…,9 (in this order) such that the result is 100.
  2.  
  3. package defaultpackage;
  4.  
  5. import javax.script.ScriptEngine;
  6. import javax.script.ScriptEngineManager;
  7. import javax.script.ScriptException;
  8.  
  9. class Main {
  10. static int[] pos_counter = {0,0,0,0,0,0,0,0};
  11.  
  12. static Boolean running = true;
  13.  
  14. public static void main(String[] args) {
  15. int counter = 0;
  16.  
  17. while(running) {
  18. increment();
  19. oobcheck();
  20. try {
  21. counter = (int) calculate(printeq());
  22. } catch (ScriptException e2) {
  23. e2.printStackTrace();
  24. }
  25. if(counter==100) {
  26. System.out.print(printeq());
  27. System.out.print("=");
  28. System.out.println(100);
  29. }
  30. }
  31. }
  32.  
  33. public static int[] increment(){
  34. int stepcounter = 7;
  35. pos_counter[7]++;
  36. while(stepcounter>= 0&&running){
  37.  
  38. if(pos_counter[stepcounter]==3&&running){
  39. if(stepcounter-1 == -1){
  40. running = false;
  41. } else{
  42. pos_counter[stepcounter-1]++;
  43. pos_counter[stepcounter]=0;
  44. }
  45. }
  46. stepcounter--;
  47. }
  48. return pos_counter;
  49. }
  50.  
  51. public static double calculate(String s) throws ScriptException{
  52. ScriptEngineManager mgr = new ScriptEngineManager();
  53. ScriptEngine engine = mgr.getEngineByName("JavaScript");
  54. return (double) engine.eval(s);
  55. }
  56.  
  57. public static void readout() {
  58. int stepcounter = 0;
  59. while(stepcounter <= 7) {
  60. System.out.print(pos_counter[stepcounter]);
  61. stepcounter++;
  62. }
  63. System.out.println("");
  64. }
  65.  
  66. public static String printeq() {
  67. int stepcounter = 0;
  68. String read = "";
  69. while(stepcounter <= 7) {
  70. read = "" + read + (stepcounter+1);
  71. if(pos_counter[stepcounter] == 0){
  72. }else if(pos_counter[stepcounter] == 1) {
  73. read = read + "+";
  74. }else if(pos_counter[stepcounter] == 2) {
  75. read = read + "-";
  76. }
  77. stepcounter++;
  78. }
  79. read = read + "9";
  80. return read;
  81. }
  82.  
  83. public static void oobcheck(){
  84. int stepcounter = 0;
  85. Boolean checker = true;
  86. while(checker&&stepcounter <= 7) {
  87. if(pos_counter[stepcounter] == 2) {
  88. stepcounter++;
  89. checker = true;
  90. }else{
  91. checker = false;
  92. }
  93. if(stepcounter==7){
  94. if(pos_counter[7] == 2) {
  95. running = false;
  96. }
  97. }
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement