代码改变世界

一个求24点的算法

2009-11-09 01:07  Kevin Pan  阅读(594)  评论(0编辑  收藏  举报

一个求24点的算法

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _24Point
{
    
class Program
    
{
        
/*
         * a b c d (1 - 10)
         * x y z (+ - * /)
         * axbyczd有无种情况,逆波兰表达式
         * (axb)y(czd)  abxcdzy
         * ((axb)yc)zd  abxcydz
         * (ax(byc))zd  abcyxdz
         * ax(by(czd))  abcdzyx
         * ax((byc)zd)  abcydzx
         
*/

        
static void Main(string[] args)
        
{
            
double[] nums = new double[45551 };
            
string x = "", y = "", z = "";
            
double a = 0, b = 0, c = 0, d = 0;
            
for (int i = 0; i < 4; i++)
            
{
                
for (int j = 0; j < 4; j++)
                
{
                    
for (int k = 0; k < 4; k++)
                    
{
                        x 
= i == 0 ? "+" : i == 1 ? "-" : i == 2 ? "*" : "/";
                        y 
= j == 0 ? "+" : j == 1 ? "-" : j == 2 ? "*" : "/";
                        z 
= k == 0 ? "+" : k == 1 ? "-" : k == 2 ? "*" : "/";
                        
for (int ia = 0; ia < 4; ia++)
                        
{
                            a 
= nums[ia];
                            
for (int ib = 0; ib < 4; ib++)
                            
{
                                
if (ia == ib)
                                
{
                                    
continue;
                                }

                                b 
= nums[ib];
                                
for (int ic = 0; ic < 4; ic++)
                                
{
                                    
if (ic == ia || ic == ib)
                                    
{
                                        
continue;
                                    }

                                    c 
= nums[ic];
                                    d 
= nums[6 - ia - ib - ic];
                                    
if (Math.Abs(Calc(Calc(a, b, x), Calc(c, d, z), y) - 24d) < 0.01)
                                    
{
                                        System.Console.WriteLine(
"({0}{4}{1}){5}({2}{6}{3}) = 24", a, b, c, d, x, y, z);
                                    }

                                    
if (Math.Abs(Calc(Calc(Calc(a, b, x), c, y), d, z) - 24d) < 0.01)
                                    
{
                                        System.Console.WriteLine(
"(({0}{4}{1}){5}{2}){6}{3} = 24", a, b, c, d, x, y, z);
                                    }

                                    
if (Math.Abs(Calc(Calc(a, Calc(b, c, y), x), d, z) - 24d) < 0.01)
                                    
{
                                        System.Console.WriteLine(
"({0}{4}({1}{5}{2})){6}{3} = 24", a, b, c, d, x, y, z);
                                    }

                                    
if (Math.Abs(Calc(a, Calc(b, Calc(c, d, z), y), x) - 24d) < 0.01)
                                    
{
                                        System.Console.WriteLine(
"{0}{4}({1}{5}({2}{6}{3})) = 24", a, b, c, d, x, y, z);
                                        
//System.Console.WriteLine(Math.Abs(Calc(a, Calc(b, Calc(c, d, z), y), x)));
                                    }

                                    
if (Math.Abs(Calc(a, Calc(Calc(b, c, y), d, z), x) - 24d) < 0.01)
                                    
{
                                        System.Console.WriteLine(
"{0}{4}(({1}{5}{2}){6}{3}) = 24", a, b, c, d, x, y, z);
                                    }

                                }

                            }

                        }


                    }

                }

            }

        }


        
static double Calc(double a, double b, string oper)
        
{
            
double result = 0;
            
switch (oper)
            
{
                
case "+":
                    result 
= a + b; break;
                
case "-":
                    result 
= a - b; break;
                
case "*":
                    result 
= a * b; break;
                
case "/":
                    result 
= a / b; break;
                
default: result = 0break;
            }

            
return result;
        }

    }

}