[算法设计与分析]快算24 解题报告

2018-06-17  本文已影响0人  vouv

Problem

一副牌,除了大小王之外还有52张,从1到13每个数目各有四张。要求设计一个程序,对于任意给出52张牌中的四张,运用+-×÷四种运算来判断能否每个数只能用一次,但不能不用,算出24来。注意,给出的4个数是无序的。

test input

1 1 1 1
2 3 4 1
7 2 1 1

test output

no
yes
yes

ac code

//
//  main.cpp
//  求得24
//
//  Created by jetviper on 2017/3/24.
//  Copyright © 2017年 awlsx. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <math.h>
double numbers[5];
int selected[5];
bool gotIt = false;
void search(double lastResult,int deep){
    if (gotIt) {
        return;
    }
    if (deep == 5) {
        if(fabs(lastResult - 24) < 0.00001){
            
            gotIt = true;
        }
        return;
    }
   
    
    for (int i=0; i<4; i++) {
        
        if (selected[i] == 1)continue;
        

            //searching
        
        
        selected[i] = 1;
        
            search(lastResult + numbers[i], deep+1);
        
            search(lastResult - numbers[i], deep+1);
        
            search(numbers[i] - lastResult, deep+1);
        
            search(lastResult * numbers[i], deep+1);
        
            search(lastResult / numbers[i], deep+1);
        
            if(lastResult!=0)search(numbers[i] / lastResult, deep+1);
        selected[i] = 0;
                
        
    }
    
    
    if (deep == 3) {
        double tmp[3];
        int k=0;
        for (int i=0; i<4; i++) {
            if (selected[i] == 0) {
                tmp[k++] = numbers[i];
            }
        }
        

        
        search(lastResult + (tmp[1] * tmp[0]), 5);
        search(lastResult + (tmp[1] / tmp[0]), 5);
        
        search(lastResult + (tmp[0] / tmp[1]), 5);
        
        search(lastResult - (tmp[1] * tmp[0]), 5);
        search(lastResult - (tmp[1] / tmp[0]), 5);
        search(lastResult - (tmp[0] / tmp[1]), 5);
        search((tmp[1] * tmp[0]) - lastResult, 5);
        search((tmp[1] / tmp[0]) - lastResult, 5);
        search((tmp[0] / tmp[1]) - lastResult, 5);

        
        search(lastResult * (tmp[0] + tmp[1]), 5);
        search(lastResult * (tmp[0] - tmp[1]), 5);
        search(lastResult * (tmp[1] - tmp[0]), 5);
        
        search(lastResult/(tmp[0] + tmp[1]), 5);
        if((tmp[0] - tmp[1])!=0)search(lastResult/(tmp[0] - tmp[1]), 5);
        if((tmp[0] - tmp[1])!=0)search(lastResult/(tmp[1] - tmp[0]), 5);
        
        if(lastResult != 0){
        search((tmp[0] + tmp[1]) / lastResult, 5);
        search((tmp[0] - tmp[1]) / lastResult, 5);
        search((tmp[1] - tmp[0]) / lastResult, 5);
        }

        
    }
    
    
    

    
}

int main(int argc, const char * argv[]) {
    
//    for (int a =3; a<14; a++)
//        for (int b =a; b<14; b++)
//            for (int c =b; c<14; c++)
//                for (int d =c; d<14; d++){
//                    
//                numbers[0]=a,numbers[1]=b,numbers[2]=c,numbers[3]=d;
    

    while(scanf("%lf",&numbers[0])!=EOF){
        gotIt = false;
        for (int i=0; i<5; i++) {
            selected[i] = 0;
        }
        

        for (int i=1; i<4; i++) {
            scanf("%lf",&numbers[i]);
        }
        
//        printf("\n");
//        for (int i=0; i<4; i++) {
//            printf(" %d ",numbers[i]);
//        }
        
        for (int i=0; i<4; i++) {
                selected[i] = 1;
                search(numbers[i], 2);
                selected[i] = 0;
            }

        if(gotIt){
            printf("yes\n");
            
        }else{
            printf("no\n");
        }
        
    }
    

    
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读