⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10020.cpp

📁 算24点
💻 CPP
字号:
#include <iostream>

using namespace std;

int a[5],tmp,t,sign[5],ans[5];
char out[] = "0+-*/";
bool used[5];
int chose[5];

void Getinfo(){
     freopen("test.in","r",stdin);
     freopen("test.out","w",stdout);
     for(int i=1;i<=4;i++)
      cin >> a[i];     
}
 
int calc(int x,int y,int p){
    switch(p){
     case 1 : return x + y;
     case 2 : return x - y;
     case 3 : return x * y;
     case 4 : return x / y;
    }
}

void output(){
     cout << chose[1] << out[sign[1]] << chose[2] <<  '=' << ans[1] << endl;
     cout << ans[1] << out[sign[2]] << chose[3] << '=' << ans[2] << endl;
     cout << ans[2] << out[sign[3]] << chose[4] << '=' << 24 << endl;
     exit(0);
}


void output1(){
     cout << chose[1] << out[sign[1]] << chose[2] <<  '=' << ans[1] << endl;
     cout << chose[3] << out[sign[2]] << chose[4] << '=' << ans[2] << endl;
     cout << ans[1] << out[sign[3]] << ans[2] << '=' << 24 << endl;
     exit(0);
}

void output2(){
     cout << chose[2] << out[sign[1]] << chose[3] <<  '=' << ans[1] << endl;
     cout << chose[1] << out[sign[2]] << ans[1] << '=' << ans[2] << endl;
     cout << ans[2] << out[sign[3]] << chose[4] << '=' << 24 << endl;
     exit(0);
}

void output3(){
     cout << chose[2] << out[sign[1]] << chose[3] <<  '=' << ans[1] << endl;
     cout << ans[1] << out[sign[2]] << chose[4] << '=' << ans[2] << endl;
     cout << chose[1] << out[sign[3]] << ans[2] << '=' << 24 << endl;
     exit(0);    
}

void output4(){
     cout << chose[3] << out[sign[1]] << chose[4] <<  '=' << ans[1] << endl;
     cout << chose[2] << out[sign[2]] << ans[1] << '=' << ans[2] << endl;
     cout << chose[1] << out[sign[3]] << ans[2] << '=' << 24 << endl;
     exit(0);
}

void deal(){
     for(int i=1;i<=4;i++)
      for(int j=1;j<=4;j++)
       for(int k=1;k<=4;k++){
        if(i == 4 && ( chose[1] < chose[2] || chose[1] % chose[2] != 0  )) goto next;
        ans[1] = calc(chose[1],chose[2],i);
        //if( ans[1] < 0 ) goto next;
        
        if(j == 4 && ( ans[1] < chose[3] || ans[1] % chose[3] != 0 )) goto next;
        ans[2] = calc(ans[1],chose[3],j);
        //if( ans[2] < 0 ) goto next;
        
        if(k == 4 && ( ans[2] < chose[4] || ans[2] % chose[4] != 0 )) goto next;
        ans[3] = calc(ans[2],chose[4],k);
        //if( ans[3] < 0 ) goto next;
        
        if( ans[3] == 24 ){ 
         sign[1] = i;
         sign[2] = j;
         sign[3] = k;
         output();
        }
        
        next:
        if(i == 4 && ( chose[1] < chose[2] || chose[1] % chose[2] != 0 )) goto next1;
        ans[1] = calc(chose[1],chose[2],i);
        //if( ans[1] < 0 ) goto next1;
        
        if(j == 4 && ( chose[3] < chose[4] || chose[3] % chose[4] != 0 )) goto next1;
        ans[2] = calc(chose[3],chose[4],j);
        //if( ans[2] < 0 ) goto next1;
        
        if(k == 4 && (ans[2] == 0 || ans[1] < ans[2] || ans[1] % ans[2] != 0 )) goto next1;
        ans[3] = calc(ans[1],ans[2],k);
        //if( ans[3] < 0 ) goto next1;
        
        if( ans[3] == 24 ){
         sign[1] = i;
         sign[2] = j;
         sign[3] = k;
         output1();    
        }
        
        next1:
        if(i == 4 && ( chose[2] < chose[3] || chose[2] % chose[3] != 0 )) goto next2;
        ans[1] = calc(chose[2],chose[3],i);
        //if( ans[1] < 0 ) goto next2;
        
        if(j == 4 && ( ans[1] == 0 || chose[1] < ans[1] || chose[1] % ans[1] != 0 )) goto next2;
        ans[2] = calc(a[1],ans[1],j);
        //if( ans[2] < 0 ) goto next2;
        
        if(k == 4 && ( ans[2] < chose[4] || ans[2] % chose[4] != 0 )) goto next2;
        ans[3] = calc(ans[2],chose[4],k);
        //if( ans[3] < 0 ) goto next2;
        
        if( ans[3] == 24 ){
         sign[1] = i;
         sign[2] = j;
         sign[3] = k;
         output2();
        }
        
        next2:
        if(i == 4 && ( chose[2] < chose[3] || chose[2] % chose[3] != 0 )) goto next3;
        ans[1] = calc(chose[2],chose[3],i);
        //if( ans[1] < 0 ) goto next3;
        
        if(j == 4 && ( ans[1] < chose[4] || ans[1] % chose[4] != 0 )) goto next3;
        ans[2] = calc(ans[1],chose[4],j);
        //if( ans[2] < 0 ) goto next3;
        
        if(k == 4 && ( ans[2] == 0 || chose[1] < ans[2] || chose[1] % ans[2] != 0 )) goto next3;
        ans[3] = calc(chose[1],ans[2],k);
        //if( ans[3] < 0 ) goto next3;
        
        if( ans[3] == 24 ){
         sign[1] = i;
         sign[2] = j;
         sign[3] = k;
         output3();    
        }
        
        next3:
        if(i == 4 && ( chose[3] < chose[4] || chose[3] % chose[4] != 0 )) continue;
        ans[1] = calc(chose[3],chose[4],i);
        //if( ans[1] < 0 ) continue;
        
        if(j == 4 && (ans[1] == 0 || chose[2] < ans[1] || chose[2] % ans[1] != 0 )) continue;
        ans[2] = calc(chose[2],ans[1],j);
        //if( ans[2] < 0 ) continue;
        
        if(k == 4 && (ans[2] == 0 || chose[1] < ans[2] || chose[1] % ans[2] != 0 )) continue;
        ans[3] = calc(chose[1],ans[2],k);
        //if( ans[3] < 0 ) continue;
        
        if( ans[3] == 24 ){
         sign[1] = i;
         sign[2] = j;
         sign[3] = k;
         output4();
        }
       }    
}

void dfs(int b){
     for(int i=1;i<=4;i++){
      if( !used[i] ){
       used[i] = 1;
       chose[b] = a[i];
       if( b == 4 ) deal(); 
       else dfs(b+1);
       used[i] = 0;    
      }
     }     
}

int main(){
    Getinfo();
    dfs(1);
    cout << "No answer!";
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -