| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 541 人关注过本帖, 1 人收藏
标题:24的C++算法实现
只看楼主 加入收藏
aiwowunai
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-4-12
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:4 
24的C++算法实现
求1到10这10个数字中随即拿出4个,利用C++算法实现计算后等于24的计算公式的算法
搜索更多相关主题的帖子: 算法 
2010-04-12 11:43
秀痘魔导士
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:250
专家分:1150
注 册:2009-12-23
收藏
得分:6 
程序代码:
#include <fstream>
#include <algorithm>
#include <string>
#include <sstream>
#include <list>
#include <cmath>
#include <climits>
#include <bitset>
using namespace std; 

const char* INPUT_FILE  = "game.in";
const char* OUTPUT_FILE = "game.out";
const int NUMBER_COUNT  = 6;
const int STATE_COUNT   = (1 << NUMBER_COUNT);
const int MAX_NUMBER    = 100;
const int MAX_EXPECTION = 1000;
const int MAX_VALUE             = MAX_EXPECTION * MAX_NUMBER; 

struct Node {
        int value;
        int left, right;       
        int leftvalue, rightvalue;
        char opr;
}; 

typedef list<Node> NodeList; 

struct State {
        bitset<MAX_VALUE+10> exist;
        NodeList nodelist;
}; 

int number[NUMBER_COUNT], expection;
State state[STATE_COUNT]; 

void ReadData()
{
        ifstream fin(INPUT_FILE);
       
        for (int i = 0; i < NUMBER_COUNT; i++) {
                fin >> number[i];
        }
        fin >> expection;
} 

void Init()
{
        Node node ;
        for (int i = 0; i < NUMBER_COUNT; i++) {
                node.value              = number[i];
                node.left = node.right = -1;
                state[(1 << i)].nodelist.push_back(node);
                state[(1 << i)].exist[node.value] = true;
        }
} 

void Merge(int a, int b, int x)
{      
        Node node;     
        NodeList::const_iterator i, j; 

        for (i = state[a].nodelist.begin(); i != state[a].nodelist.end();
i++) {
                for (j = state[b].nodelist.begin(); j != state[b].nodelist.en 

d(); j++)
{                                     
                        node.value = (*i).value + (*j).value;
                        node.left  = a;
                        node.right = b;                
                        node.leftvalue  = (*i).value;
                        node.rightvalue = (*j).value;
                        node.opr   = '+';
                        if ( (node.value <= MAX_VALUE) && (!state[x].exist[no 

de.value]) ) {
                                state[x].nodelist.push_back(node);
                                state[x].exist[node.value] = true;
                        } 

                        ///////////////////////////////////////////////////// 

                        double tmp = double((*i).value) * double((*j).value); 

                        if (tmp < INT_MAX) {
                                node.value = (*i).value * (*j).value;
                                node.left  = a;
                                node.right = b;
                                node.leftvalue  = (*i).value;
                                node.rightvalue = (*j).value;
                                node.opr   = '*';
                                if ( (node.value <= MAX_VALUE) && (!state[x]. 

exist[node.value]) )
{
                                        state[x].nodelist.push_back(node);
                                        state[x].exist[node.value] = true;
                                }
                        } 

                        ///////////////////////////////////////////////////// 

                        if ((*i).value >= (*j).value) {
                                node.value = (*i).value - (*j).value;
                                node.left  = a;
                                node.right = b;
                                node.leftvalue  = (*i).value;
                                node.rightvalue = (*j).value;
                                node.opr   = '-';
                        } else {
                                node.value = (*j).value - (*i).value;
                                node.left  = b;
                                node.right = a;
                                node.leftvalue  = (*j).value;
                                node.rightvalue = (*i).value;
                                node.opr   = '-';
                        }
                                               
                        if ( (node.value <= MAX_VALUE) && (!state[x].exist[no 

de.value]) ) {
                                state[x].nodelist.push_back(node);
                                state[x].exist[node.value] = true;
                        } 

                        ///////////////////////////////////////////////////// 


                        if ( ((*j).value != 0) && ((*i).value >= (*j).value) &
&
                                        ((*i).value % (*j).value == 0) )
                        {
                                node.value = (*i).value / (*j).value;
                                node.left  = a;
                                node.right = b;        
                                node.leftvalue  = (*i).value;
                                node.rightvalue = (*j).value;
                                node.opr   = '/';
                        } else if ( ((*i).value != 0) && ((*j).value >= (*i). 

value) &&
                                        ((*j).value % (*i).value == 0) )
                        {
                                node.value = (*j).value / (*i).value;
                                node.left  = b;
                                node.right = a;
                                node.leftvalue  = (*j).value;
                                node.rightvalue = (*i).value;
                                node.opr   = '/';
                        }
                                               
                        if ( (node.value <= MAX_VALUE) && (!state[x].exist[no 

de.value]) )
{                              
                                state[x].nodelist.push_back(node);
                                state[x].exist[node.value] = true;
                        }                      
                        ///////////////////////////////////////////////////// 

                }
        }
} 

void Solve()
{
        Init();
       
        for (int x = 2; x < STATE_COUNT; x++) {
                for (int i = 1; i < x; i++) {                  
                        if ( (x & i) == i ) {
                                int j = x - i;
                                if (i <= j) {
                                        Merge(i, j, x);        
                                }
                        }
                }
        }
} 

void PrintExpression(ostream& out, Node node)
{
        if (node.left == -1) {
                out << node.value;
        } else {
                NodeList::const_iterator iter;
               
                out << "("; 

                for (iter = state[node.left].nodelist.begin();
                                iter != state[node.left].nodelist.end();
                                iter++)
                {
                        if ((*iter).value == node.leftvalue) {
                                PrintExpression(out, *iter);
                                break;
                        }
                } 

                out << node.opr; 

                for (iter = state[node.right].nodelist.begin();
                                iter != state[node.right].nodelist.end();
                                iter++)
                {
                        if ((*iter).value == node.rightvalue) {
                                PrintExpression(out, *iter);
                                break;
                        }
                } 

                out << ")";
        }              
} 

void Output()
{
        ofstream fout(OUTPUT_FILE); 

        int bestValue = -INT_MAX;      
        NodeList::const_iterator iter, bestIter; 

        NodeList& nodelist = state[STATE_COUNT-1].nodelist; 

        for (iter = nodelist.begin(); iter != nodelist.end(); iter++)
        {      
                if ( ((*iter).value <= expection) && (bestValue < (*iter).val 

ue) ) {
                        bestValue = (*iter).value;
                        bestIter  = iter;
                }
        }      
        fout << bestValue << endl;
        PrintExpression(fout, *bestIter );
        fout << endl;
} 

int main()
{
        ReadData();
        Solve();
        Output();
        return 0;
} 
2010-04-12 13:12
a910317930
Rank: 2
等 级:论坛游民
帖 子:5
专家分:17
注 册:2009-4-8
收藏
得分:6 
#include"stdio.h"
void main(void)
{
    int a,b,c,d;
    printf("a b c d\n");
    for(a=1;a<=10;a++)
       for(b=1;b<=10;b++)
          for(c=1;c<=10;c++)
             for(d=1;d<=10;d++)
             {
                if(a+b+c+d==10)
                    printf("%d %d %d %d\n",a,b,c,d);
             }

}
/*
求1到10这10个数字中随即拿出4个,利用C++算法实现计算后等于24的计算公式的算法
2010年4月12日 在VC++6.0上调试通过.
*/
2010-04-12 13:15
秀痘魔导士
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:250
专家分:1150
注 册:2009-12-23
收藏
得分:0 
以下是引用a910317930在2010-4-12 13:15:18的发言:

#include"stdio.h"
void main(void)
{
    int a,b,c,d;
    printf("a b c d\n");
    for(a=1;a<=10;a++)
       for(b=1;b<=10;b++)
          for(c=1;c<=10;c++)
             for(d=1;d<=10;d++)
             {
                if(a+b+c+d==10)
                    printf("%d %d %d %d\n",a,b,c,d);
             }
 
}
/*
求1到10这10个数字中随即拿出4个,利用C++算法实现计算后等于24的计算公式的算法
2010年4月12日 在VC++6.0上调试通过.
*/
这是什么?
2010-04-12 13:59
hzyzxj
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:168
注 册:2009-6-14
收藏
得分:6 
回复 2楼 秀痘魔导士
强!
2010-04-12 20:51
快速回复:24的C++算法实现
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021060 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved