Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. char conf[9]; //Para todas las configuraciones del problema
  5. bool encontro;
  6. bool used[5];
  7. int numero[5];
  8.  
  9. void verificar(){
  10. //Operadores +: 0 -: 1 *:2
  11. int resultado = 0;
  12. int n1, n2,n3,n4,n5;
  13. n1 = numero[conf[0]];
  14. n2 = numero[conf[2]];
  15. n3 = numero[conf[4]];
  16. n4 = numero[conf[6]];
  17. n5 = numero[conf[8]];
  18.  
  19. if(conf[1] == 0) resultado = n1+n2;
  20. else if(conf[1] == 1) resultado = n1-n2;
  21. else resultado = n1*n2;
  22.  
  23. if(conf[3] == 0) resultado = resultado + n3;
  24. else if(conf[3] == 1) resultado = resultado - n3;
  25. else resultado = resultado*n3;
  26.  
  27. if(conf[5] == 0) resultado = resultado+n4;
  28. else if(conf[5] == 1) resultado = resultado-n4;
  29. else resultado = resultado*n4;
  30.  
  31. if(conf[7] == 0) resultado = resultado+n5;
  32. else if(conf[7] == 1) resultado = resultado-n5;
  33. else resultado = resultado*n5;
  34.  
  35. if(resultado == 23) encontro = true;
  36. }
  37.  
  38. void bt(int pos){
  39. if(encontro) return;
  40.  
  41. if(pos==9){
  42. verificar();
  43. return;
  44. }
  45. if(pos%2==0){
  46. for(int i=0;i<5;i++){
  47. if(!used[i]){
  48. used[i] = true;
  49. conf[pos] = i;
  50. bt(pos+1);
  51. used[i] = false;
  52. conf[pos] = -1;
  53. }
  54. }
  55. }
  56. else{
  57. for(int i=0; i<3;i++){
  58. conf[pos] = i;
  59. bt(pos+1);
  60. conf[pos] = -1;
  61. }
  62. }
  63.  
  64. }
  65. int main() {
  66.  
  67. while(cin>>numero[0]>>numero[1]>>numero[2]>>numero[3]>>numero[4]){
  68. if(numero[0]==0 && numero[1]==0 && numero[2]==0 && numero[3]==0 && numero[4]==0) break;
  69. //iniciallizar
  70. encontro = false;
  71. for(int i=0;i<9;i++) conf[i] = -1;
  72. for(int i=0;i<5; ++i) used[i] = false;
  73.  
  74. //---------------------------------------------
  75. bt(0);
  76. if(encontro) cout<< "Posible\n";
  77. else cout<< "Imposible\n";
  78.  
  79. }
  80.  
  81.  
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement