Advertisement
ed_edwardaj

Untitled

Jan 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. char Op[] = {'*','+','/','-'}; double Num[4]; double ArrNum[24][4];
  4.  
  5. double FOp (char op, double a, double b) { /* Fungsi empat operasi aritmatika yang digunakan pada program utama */
  6. if (op == '+') return (double)a+b;
  7. else if (op == '-') return (double)a-b;
  8. else if (op == '/') { if (b != 0) return (double)a/b;}
  9. else if (op == '*') return (double)a*b;
  10. }
  11.  
  12. int main () {
  13. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //Mempercepat input output
  14. int no = 0; int n = 0; int Hsl = 24; double EPS = 0.0000000001; bool IsExist;
  15. for (int i = 0 ; i < 4 ; ++i) cin >> Num[i]; cout << "Solusi :" <<endl;
  16. clock_t Mulai = clock(); //Waktu Dimulai
  17. for (int a = 0 ; a < 4 ; a++){
  18. for(int b = 0 ; b <4 ; b++) {
  19. if (b != a){
  20. for (int c = 0 ; c < 4 ; c++) {
  21. if ((c != a) && (c != b)) {
  22. for (int d = 0 ; d < 4 ; d++) {
  23. if ((d !=a ) && (d != b) && ( d!= c)){
  24. ArrNum[n][0] = Num[a]; ArrNum[n][1] = Num[b];
  25. ArrNum[n][2] = Num[c]; ArrNum[n][3] = Num[d]; IsExist = false;
  26. for (int f = 0 ; f < n ; f++) {
  27. for (int g = 0 ; g < 4 ; g++) {
  28. if (g < 3) {
  29. if (ArrNum[f][g] != ArrNum[n][g]) break;
  30. } else {
  31. if (ArrNum[f][g] == ArrNum[n][g]) IsExist = true;
  32. }
  33. }
  34. if (IsExist == true) break;
  35. }
  36.  
  37. if (IsExist == false ) {
  38. n++;
  39. for (int i = 0 ; i < 4 ; i++) { // operator di posisi pertama
  40. for (int j = 0 ; j < 4 ; j++) { // operator di posisi kedua
  41. for (int k = 0; k < 4 ; k++) { // operator di posisi ketiga
  42. if (fabs(FOp(Op[k], (FOp(Op[j], (FOp(Op[i],Num[a], Num[b])), Num[c])), Num[d]) -Hsl) <= EPS) { // ((a op b) op c) op d
  43. no++; cout << no << " "<< "((" << Num[a] << Op[i] << Num[b] << ")" << Op[j] << Num[c] << ")" << Op[k] << Num[d] << endl;
  44. }
  45. if (fabs(FOp(Op[k], (FOp( Op[i], Num[a], (FOp(Op[j],Num[b], Num[c])) ) ) , Num[d])- Hsl) <= EPS) { // (a op (b op c)) op d
  46. no++; cout << no << " "<< "(" << Num[a] << Op[i] << "(" << Num[b] << Op[j] << Num[c] << "))" << Op[k] << Num[d] << endl;
  47. }
  48. if (fabs(FOp(Op[j],FOp(Op[i], Num[a], Num[b]), FOp(Op[k], Num[c], Num[d])) - Hsl ) <= EPS) { // (a op b) op (c op d)
  49. no++; cout << no << " "<< "(" << Num[a] << Op[i] << Num[b] << ")" << Op[j] << "(" << Num[c] << Op[k] << Num[d] << ")" << endl;
  50. }
  51. if (fabs(FOp(Op[i],Num[a],FOp(Op[k],FOp(Op[j],Num[b],Num[c]), Num[d])) - Hsl) <= EPS) { // a op ((b op c) op d)
  52. no++; cout << no <<" " << Num[a] << Op[i] << "((" << Num[b] << Op[j] << Num[c] << ")" << Op[k] << Num[d] << ")" << endl;
  53. }
  54. if (fabs(FOp(Op[i],Num[a],FOp(Op[j],Num[b],FOp(Op[k], Num[c],Num[d]))) - Hsl) <= EPS) { // a op (b op (c op d))
  55. no++; cout << no << " " << Num[a] << Op[i] << "(" << Num[b] << Op[j] << "(" << Num[c] << Op[k] << Num[d] << "))" << endl;
  56. } // Terdapat lima buah if-condition yang berguna untuk meletakkan semua kemungkinan '(' dan ')'
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. if (no == 0) cout << "Tidak Ada Solusi" << endl;
  69. printf("Waktu Berjalan : %.5fs\n", (double)(clock() - Mulai)/CLOCKS_PER_SEC); //Waktu Berjalan
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement