fuliver123

Đệ Quy phép toán

Oct 1st, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int cp(int num1, int num2, int type){
  6.     if (type<2){
  7.         if (type==0){
  8.             return num1+num2;
  9.         }else{
  10.             return num1-num2;
  11.         }
  12.     }else{
  13.         if (type==2){
  14.             return num1*num2;
  15.         }else{
  16.             return num1/num2;
  17.         }
  18.     }
  19. }
  20.  
  21. void Find(int number){
  22.     static int rst = number;
  23.     static int arrcp[9];
  24.     if (number == 1){
  25.         if (rst ==2016){
  26.             char p[] = {'+','-','*','/'};
  27.             for (int i=0;i<8;i++){
  28.                 cout << i+1 << p[arrcp[i+1]] << "(";
  29.             }
  30.             cout << "9";
  31.             for (int i=0;i<8;i++){
  32.                 cout << ")";
  33.             }
  34.             cout << "\n";
  35.         }
  36.         return;
  37.     }
  38.     for (int i=0;i<4;i++){
  39.         int bk = rst;
  40.         if (!(rst==0 && i==3)){
  41.             rst=cp(number-1,rst,i);
  42.         }
  43.         arrcp[number-1]=i;
  44.         Find(number-1);
  45.         rst = bk;
  46.     }
  47. }
  48.  
  49. int main(){
  50.     Find(9);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment