Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- char conf[9]; //Para todas las configuraciones del problema
- bool encontro;
- bool used[5];
- int numero[5];
- void verificar(){
- //Operadores +: 0 -: 1 *:2
- int resultado = 0;
- int n1, n2,n3,n4,n5;
- n1 = numero[conf[0]];
- n2 = numero[conf[2]];
- n3 = numero[conf[4]];
- n4 = numero[conf[6]];
- n5 = numero[conf[8]];
- if(conf[1] == 0) resultado = n1+n2;
- else if(conf[1] == 1) resultado = n1-n2;
- else resultado = n1*n2;
- if(conf[3] == 0) resultado = resultado + n3;
- else if(conf[3] == 1) resultado = resultado - n3;
- else resultado = resultado*n3;
- if(conf[5] == 0) resultado = resultado+n4;
- else if(conf[5] == 1) resultado = resultado-n4;
- else resultado = resultado*n4;
- if(conf[7] == 0) resultado = resultado+n5;
- else if(conf[7] == 1) resultado = resultado-n5;
- else resultado = resultado*n5;
- if(resultado == 23) encontro = true;
- }
- void bt(int pos){
- if(encontro) return;
- if(pos==9){
- verificar();
- return;
- }
- if(pos%2==0){
- for(int i=0;i<5;i++){
- if(!used[i]){
- used[i] = true;
- conf[pos] = i;
- bt(pos+1);
- used[i] = false;
- conf[pos] = -1;
- }
- }
- }
- else{
- for(int i=0; i<3;i++){
- conf[pos] = i;
- bt(pos+1);
- conf[pos] = -1;
- }
- }
- }
- int main() {
- while(cin>>numero[0]>>numero[1]>>numero[2]>>numero[3]>>numero[4]){
- if(numero[0]==0 && numero[1]==0 && numero[2]==0 && numero[3]==0 && numero[4]==0) break;
- //iniciallizar
- encontro = false;
- for(int i=0;i<9;i++) conf[i] = -1;
- for(int i=0;i<5; ++i) used[i] = false;
- //---------------------------------------------
- bt(0);
- if(encontro) cout<< "Posible\n";
- else cout<< "Imposible\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement