fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdexcept>
  4. #include <limits.h>
  5.  
  6. typedef std::vector<int> IB;
  7.  
  8. int CalcOne(int A, int B, int O){
  9. if (O == 0) return A + B;
  10. if (O == 1) return A - B;
  11. if (O == 2) return A*B;
  12. if (O == 3){
  13. if (B == 0){
  14. std::cout << "Invlid Number: B=0. in CalcOne!";
  15. throw std::logic_error("Divide by Zero!!!! in CalcOne");
  16. }
  17. return A / B;
  18. }
  19.  
  20. return 0;
  21. }
  22.  
  23. bool PopOne(IB& In, int Pos){
  24. In.erase(In.begin() + Pos);
  25. return 0;
  26. }
  27.  
  28. int Calc(IB Number, IB Operator){
  29.  
  30. int C = 0;
  31.  
  32. while (Number.size() > 1){//先に除算と乗算を処理
  33. C = 0;
  34. for (int i = 0; i < Operator.size(); i++){
  35. if (Operator[i] == 2 || Operator[i] == 3){
  36. Number[i]= CalcOne(Number[i], Number[i + 1], Operator[i]);
  37. PopOne(Number, i + 1);
  38. PopOne(Operator, i);
  39. C++;
  40. break;
  41. }
  42. }
  43. if (C == 0) break;
  44. }
  45. while (Number.size() > 1){//足し引きを処理
  46. Number[0] = CalcOne(Number[0], Number[1], Operator[0]);
  47. PopOne(Number, 1);
  48. PopOne(Operator, 0);
  49. }
  50.  
  51. return Number[0];
  52. }
  53.  
  54. IB Input(){
  55. IB Number;
  56. int T;
  57. std::cin.clear();
  58. std::cout << "Input Numberz if need end input to hit Enter after Enter EOF!" << std::endl;
  59. do{
  60. std::cin >> T;
  61. Number.push_back(T);
  62. } while (std::cin.eof() == false);
  63. //std::cin.ignore(INT_MAX);
  64. std::cin.clear();
  65. Number.pop_back();
  66. return Number;
  67. }
  68.  
  69. bool IncOperator(IB& Operator){
  70. Operator[Operator.size()-1]++;
  71. for (int i = Operator.size() - 1; i >= 0; i--){
  72. if (Operator[0] >=4)return true;
  73. if (Operator[i] >= 4) Operator[i - 1]++;
  74. Operator[i] %= 4;
  75. }
  76.  
  77. return false;;
  78. }
  79.  
  80. bool ShowFormula(IB& N, IB& O){
  81. char Op[] = "+-*/";
  82. for (int i = 0; i < N.size(); i++){
  83. if (N.size()>i) std::cout << N[i];
  84. if (O.size()>i) std::cout << Op[O[i]];
  85. }
  86. return true;
  87. }
  88.  
  89. int main(){
  90.  
  91. IB Number;
  92. IB Operator;
  93.  
  94. int Answer = 0;
  95. int Response = 0;
  96. int Count = 0;
  97.  
  98. Number = Input();
  99. std::cout << "Input Needed number of Answer" << std::endl;
  100. std::cin >> Answer;
  101. std::cout << "Notice:1/2 = 0 like.by integer Value" << std::endl;
  102. Operator.resize(Number.size() - 1);
  103. while (1){
  104. Response = Calc(Number, Operator);
  105. if (Response == Answer){
  106. ShowFormula(Number, Operator);
  107. std::cout << "=>" <<Response<< std::endl;
  108. Count++;
  109. }
  110. if (IncOperator(Operator) == true) break;
  111. }
  112.  
  113. if (Count != 0){
  114. std::cout << "i find "<<Count<<" Pattern!?" << std::endl;
  115. }
  116. else{
  117. std::cout << "i cant find any Pattern... i am sorry!" << std::endl;
  118. }
  119.  
  120. return 0;
  121.  
  122. }
Success #stdin #stdout 0s 3436KB
stdin
1 2 3 4 5
10
stdout
Input Numberz if need end input to hit Enter after Enter EOF!
Input Needed number of Answer
Notice:1/2 = 0 like.by integer Value
1+2-3+4/5=>0
1+2-3-4/5=>0
1+2/3+4-5=>0
1-2/3+4-5=>0
1*2-3-4+5=>0
1*2-3*4/5=>0
1*2*3/4/5=>0
1*2/3+4/5=>0
1*2/3-4/5=>0
1*2/3*4*5=>0
1*2/3*4/5=>0
1*2/3/4*5=>0
1*2/3/4/5=>0
1/2+3/4*5=>0
1/2+3/4/5=>0
1/2-3/4*5=>0
1/2-3/4/5=>0
1/2*3+4/5=>0
1/2*3-4/5=>0
1/2*3*4*5=>0
1/2*3*4/5=>0
1/2*3/4*5=>0
1/2*3/4/5=>0
1/2/3+4/5=>0
1/2/3-4/5=>0
1/2/3*4*5=>0
1/2/3*4/5=>0
1/2/3/4*5=>0
1/2/3/4/5=>0
i find 29 Pattern!?