返回   cpper编程论坛 > 技术杂烩
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

技术杂烩 找不到地方的技术问题?这里!

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2004-06-26
lonelylover 的头像
初级会员
 
注册日期: 2003-11-11
住址: 苏州
帖子: 7
lonelylover 正向着好的方向发展
发送 MSN 消息给 lonelylover 发送 Yahoo! 消息给 lonelylover
默认 [普通]请教:如何用程序实现我们平时玩的加减乘除24

我想用程序实现我们平时玩的加减乘除24 ,设计目标是输入4个数字 给出能不能得到24,如果能 输出实现方案 及 共有几种方案

我想用最笨的 方法 直接用一层层嵌套循环 还不知道能不能行事。可我实在数学底子太弱,希望得到各位大虾的指点,这里多谢了
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2004-06-26
高级会员
 
注册日期: 2002-09-16
帖子: 1,087
文章: 1
SpitFire 正向着好的方向发展
默认

pora做过一个,找他吧

偶做的早没了
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2004-06-26
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,138
文章: 20
polyrandom 正向着好的方向发展
默认

可以的,我写过,结果请见
http://www.allaboutprogram.com/viewtopic.php?t=174
这其实就是穷举法(当然有更简单的方法)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2004-06-26
lonelylover 的头像
初级会员
 
注册日期: 2003-11-11
住址: 苏州
帖子: 7
lonelylover 正向着好的方向发展
发送 MSN 消息给 lonelylover 发送 Yahoo! 消息给 lonelylover
默认

多谢pora和SpitFire 及时解答, 不过还是很想知道pora说的(当然有更简单的方法)如何实现,我可是想破脑袋也想不起来其他的方法了,或许要用到很多的数学知识吧 ,冀望各位给一个那个更好的方法 期待中~~~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #5 (permalink)  
旧 2004-07-10
高级会员
 
注册日期: 2003-11-04
帖子: 210
pomb 正向着好的方向发展
发送 MSN 消息给 pomb
默认

我的一点想法:
首先最终结果是两个数的双目操作,就是A 加减乘除 B = 24。
乘法是比较好的,对24分解因数,对每一对再进行分解,能算出这样的因数对的就满足条件。
除法就比较麻烦,从24、48、72、96里选,也就是说,对1、2、3、4这四个数,分别检验剩下三个数的四则运算等于24的组合——可以用递归。
加法的做法跟乘法差不多,一共13对(0-24,1-23……),递归。
减法是最讨厌的,从24到100都要考虑……没想到啥好办法。

空想的,请各位达淫猛烈批评。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #6 (permalink)  
旧 2004-07-10
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,138
文章: 20
polyrandom 正向着好的方向发展
默认

代码:
#include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; typedef __int64 BigInteger; typedef unsigned int Index; typedef double Number; class CombinationIterator { vector<Index> mCurrent; Index mN; Index mM; static BigInteger factorial( Index n ) { BigInteger fact = 1; for( Index i = 2; i <= n; ++i ) fact *= i; return fact; } public: CombinationIterator(): mN( 0 ), mM( 0 ) {} CombinationIterator( Index n, Index m ) : mN( n ), mM( m ), mCurrent( (vector<Index>::size_type) m ) { if( m > n || n < 1 || m < 1 ) throw 1; for( Index i = 0; i < m; ++i ) mCurrent[ i ] = i; } void operator ++ () { if( mM == 0 ) throw 1; if( mCurrent[ 0 ] == mN - mM ) { mN = mM = 0; return; } Index i = mM - 1; while( mCurrent[ i ] == mN - mM + i ) --i; mCurrent[ i ] = mCurrent[ i ] + 1; for( Index j = i + 1; j < mM; ++j ) mCurrent[ j ] = mCurrent[ i ] + j - i; } const vector<Index>& operator* () const { return mCurrent; } bool operator == ( const CombinationIterator& that ) const { if( mM == that.mM && mM == 0 ) return true;// both end return mM == that.mM && mN == that.mN && mCurrent == that.mCurrent; } bool operator != ( const CombinationIterator& that ) const { return !( *this == that ); } }; template<typename T> class VectorCombinationIterator { vector<T> mVector; Index mCurrentM; CombinationIterator mIter; public: VectorCombinationIterator() {} VectorCombinationIterator(const vector<T>& v) : mVector( v ), mCurrentM( 1 ), mIter( (Index) v.size(), mCurrentM ) {} void operator ++ () { ++mIter; if( mIter == CombinationIterator() && mCurrentM < mVector.size() ) { ++mCurrentM; mIter = CombinationIterator( (Index) mVector.size(), mCurrentM ); } } pair< vector<T>, vector<T> > operator* () const { const vector<Index>& current = *mIter; pair< vector<T>, vector<T> > result; for( vector<T>::size_type i = 0; i < mVector.size(); ++i ) if( find( current.begin(), current.end(), i ) != current.end() ) result.first.push_back( mVector[i] ); else result.second.push_back( mVector[i] ); return result; } bool operator == ( const VectorCombinationIterator& that ) const { if( mIter == that.mIter && mIter == CombinationIterator() ) return true;// both end return mCurrentM == that.mCurrentM && mVector == that.mVector && mIter == that.mIter; } bool operator != ( const VectorCombinationIterator& that ) const { return !( *this == that ); } }; static char gOperatorChars[]={'+','-','*','/'}; struct ExpressionItem { Number mValue; char mOperator; ExpressionItem* mLeftChild; ExpressionItem* mRightChild; ExpressionItem(){} ExpressionItem( Number v, char oper, ExpressionItem* left, ExpressionItem* right ) : mValue( v ), mOperator( oper ), mLeftChild( left ), mRightChild( right ) {} static vector<ExpressionItem> mPool; static ExpressionItem* alloc( Number v, char oper, ExpressionItem* left, ExpressionItem* right ) { mPool.push_back( ExpressionItem( v, oper, left, right ) ); return &*mPool.rbegin(); } }; vector<ExpressionItem> ExpressionItem::mPool( 1024*1024 ); vector<ExpressionItem*> getPossibleResult( const vector<ExpressionItem*>& left, const vector<ExpressionItem*>& right ) { vector<ExpressionItem*> result; for( vector<ExpressionItem*>::const_iterator liter = left.begin(); liter != left.end() ; ++liter ) for( vector<ExpressionItem*>::const_iterator riter = right.begin(); riter != right.end() ; ++riter ) { result.push_back( ExpressionItem::alloc( (*liter)->mValue + (*riter)->mValue, '+', *liter, *riter ) ); result.push_back( ExpressionItem::alloc( (*liter)->mValue - (*riter)->mValue, '-', *liter, *riter ) ); result.push_back( ExpressionItem::alloc( (*liter)->mValue * (*riter)->mValue, '*', *liter, *riter ) ); if( (*riter)->mValue != 0.0 ) result.push_back( ExpressionItem::alloc( (*liter)->mValue / (*riter)->mValue, '/', *liter, *riter ) ); } return result; } vector<ExpressionItem*> getPossibleResult( const vector<ExpressionItem*>& expItems ) { if( expItems.size() == 1 ) return expItems; vector<ExpressionItem*> result; VectorCombinationIterator<ExpressionItem*> iter( expItems ); while( iter != VectorCombinationIterator<ExpressionItem*>() ) { pair< vector<ExpressionItem*>, vector<ExpressionItem*> > p = *iter; if( p.first.size() != 0 && p.second.size() != 0 ) { vector<ExpressionItem*> items = getPossibleResult( getPossibleResult( p.first ), getPossibleResult( p.second ) ); result.insert( result.end(), items.begin(), items.end() ); } ++iter; } return result; } vector<ExpressionItem*> getPossibleResult( const vector<Number>& numbers ) { vector<ExpressionItem*> expItems; for( vector<Number>::const_iterator iter = numbers.begin(); iter != numbers.end(); ++iter ) expItems.push_back( ExpressionItem::alloc( *iter, 0, NULL, NULL ) ); return getPossibleResult( expItems ); } void printExpression( ExpressionItem* item, char oper, bool isLeftChild ) { if( item->mLeftChild == NULL ) { cout<<item->mValue; } else if( ( item->mOperator == '+' || item->mOperator == '-' ) && ( oper == '*' || oper == '/' ) ) { cout<<"( "; printExpression( item->mLeftChild, item->mOperator, true ); cout<<" "; cout<<item->mOperator; cout<<" "; printExpression( item->mRightChild, item->mOperator, false ); cout<<" )"; } else if( !isLeftChild && ( ( ( item->mOperator == '+' || item->mOperator == '-' ) && oper == '-' ) || ( ( item->mOperator == '*' || item->mOperator == '/' ) && oper == '/' ) ) ) { cout<<"( "; printExpression( item->mLeftChild, item->mOperator, true ); cout<<" "; cout<<item->mOperator; cout<<" "; printExpression( item->mRightChild, item->mOperator, false ); cout<<" )"; } else { printExpression( item->mLeftChild, item->mOperator, true ); cout<<" "; cout<<item->mOperator; cout<<" "; printExpression( item->mRightChild, item->mOperator, false ); } } void printExpression( ExpressionItem* item ) { if( item->mLeftChild != NULL ) { printExpression( item->mLeftChild, item->mOperator, true ); cout<<" "; cout<<item->mOperator; cout<<" "; printExpression( item->mRightChild, item->mOperator, false ); } else cout<<item->mValue; } int main() { for( int i = 1; i <= 10; ++i ) for( int j = 1; j <= 10; ++j ) for( int k = 1; k <= 10; ++k ) for( int l = 1; l <= 10; ++l ) { if( i > j || j > k || k > l ) continue; ExpressionItem::mPool.clear(); ExpressionItem::mPool.reserve( 1024*1024 ); vector<Number> vn; vn.push_back( i ); vn.push_back( j ); vn.push_back( k ); vn.push_back( l ); vector<ExpressionItem*> exps = getPossibleResult( vn ); for( int m = 0; m < exps.size(); ++m ) if( exps[ m ]->mValue >= 23.999 && exps[ m ]->mValue <= 24.001 ) { cout<<i<<','<<j<<','<<k<<','<<l<<" \t---\t"; printExpression( exps[ m ] ); cout<<" = "<<exps[ m ]->mValue<<endl; break; } // if( m == exps.size() ) // cout<<i<<','<<j<<','<<k<<','<<l<<" have no possible result"<<endl; } return 0; }
其实参照那个9个9的题目,对表达式的元素做迭代就可以了。当然,这个算法远未优化,但是在我的电脑上,所有的4个数24点瞬间就可以算出来。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #7 (permalink)  
旧 2004-07-11
高级会员
 
注册日期: 2002-09-15
帖子: 2,531
ajoo 正向着好的方向发展
默认

其实也不是简单的穷举。这算是算法里很著名的动态编程(或者叫动态规划?)


四个数a,b,c,d

先找出所有两个一组的可能计算结果集,共六种组合方式,每种方式一个可能结果集,把结果存入一个map。

然后找出所有三个一组的可能结果集,这里面自然就重用了两个一组的所有结果集,结果继续存入map。

如此继续,直到找出n个一组的可能结果集就行了。然后就筛吧。

对了,pora,你这里用double,虽然简单了,不会有误差问题?有没有可能漏掉x/y*y的情况?

对了。前段时间看见一篇用模板做nondeterministic程序的,很有趣。也许可以用gp做静态的24点哦。
不过动态编程只怕就不行了。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #8 (permalink)  
旧 2004-07-11
高级会员
 
注册日期: 2003-11-11
帖子: 147
ilovecpp 正向着好的方向发展
默认

引用:
作者: ajoo
对了。前段时间看见一篇用模板做nondeterministic程序的,很有趣。也许可以用gp做静态的24点哦。
不过动态编程只怕就不行了。
为什么不行呢?虽然integral constant expression不能产生用户可见的副作用,但编译器的符号表是实实在在地改变了。由于编译器对特定的模板参数只作一次实例化,不仅能做DP,实际上我们拥有的是一般性的函数值缓存能力,就像maple等CAS系统提供的那样。符号表这个关联存储,应该还是很能挖掘一番的。就以最简单的fibonacci数列为例:
代码:
test.cpp: template <int n> struct fibonacci { static int const value = fibonacci<n-1>::value+ fibonacci<n-2>::value; }; template<> struct fibonacci<0> { static int const value = 1; }; template<> struct fibonacci<1> { static int const value = 1; }; template <int n> struct unknown {}; template <int n> struct print_fibonacci { static int const value = print_fibonacci<n-1>::value + unknown<fibonacci<n>::value>::value; }; template <> struct print_fibonacci<-1> { static int const value = 0; }; int const v=print_fibonacci<LAST>::value; d:\home\prog>gcc -c test.cpp -pedantic -DLAST=45 2>&1 | grep unknown test.cpp:25: error: `value' is not a member of `unknown<1836311903>' test.cpp:25: error: `value' is not a member of `unknown<1134903170>' test.cpp:25: error: `value' is not a member of `unknown<701408733>' test.cpp:25: error: `value' is not a member of `unknown<433494437>' test.cpp:25: error: `value' is not a member of `unknown<267914296>' test.cpp:25: error: `value' is not a member of `unknown<165580141>' test.cpp:25: error: `value' is not a member of `unknown<102334155>' test.cpp:25: error: `value' is not a member of `unknown<63245986>' test.cpp:25: error: `value' is not a member of `unknown<39088169>' test.cpp:25: error: `value' is not a member of `unknown<24157817>' test.cpp:25: error: `value' is not a member of `unknown<14930352>' test.cpp:25: error: `value' is not a member of `unknown<9227465>' test.cpp:25: error: `value' is not a member of `unknown<5702887>' test.cpp:25: error: `value' is not a member of `unknown<3524578>' test.cpp:25: error: `value' is not a member of `unknown<2178309>' test.cpp:25: error: `value' is not a member of `unknown<1346269>' test.cpp:25: error: `value' is not a member of `unknown<832040>' test.cpp:25: error: `value' is not a member of `unknown<514229>' test.cpp:25: error: `value' is not a member of `unknown<317811>' test.cpp:25: error: `value' is not a member of `unknown<196418>' test.cpp:25: error: `value' is not a member of `unknown<121393>' test.cpp:25: error: `value' is not a member of `unknown<75025>' test.cpp:25: error: `value' is not a member of `unknown<46368>' test.cpp:25: error: `value' is not a member of `unknown<28657>' test.cpp:25: error: `value' is not a member of `unknown<17711>' test.cpp:25: error: `value' is not a member of `unknown<10946>' test.cpp:25: error: `value' is not a member of `unknown<6765>' test.cpp:25: error: `value' is not a member of `unknown<4181>' test.cpp:25: error: `value' is not a member of `unknown<2584>' test.cpp:25: error: `value' is not a member of `unknown<1597>' test.cpp:25: error: `value' is not a member of `unknown<987>' test.cpp:25: error: `value' is not a member of `unknown<610>' test.cpp:25: error: `value' is not a member of `unknown<377>' test.cpp:25: error: `value' is not a member of `unknown<233>' test.cpp:25: error: `value' is not a member of `unknown<144>' test.cpp:25: error: `value' is not a member of `unknown<89>' test.cpp:25: error: `value' is not a member of `unknown<55>' test.cpp:25: error: `value' is not a member of `unknown<34>' test.cpp:25: error: `value' is not a member of `unknown<21>' test.cpp:25: error: `value' is not a member of `unknown<13>' test.cpp:25: error: `value' is not a member of `unknown<8>' test.cpp:25: error: `value' is not a member of `unknown<5>' test.cpp:25: error: `value' is not a member of `unknown<3>' test.cpp:25: error: `value' is not a member of `unknown<2>' test.cpp:25: error: `value' is not a member of `unknown<1>' test.cpp:25: error: `value' is not a member of `unknown<1>'
每个fibonacci<n>实例最多求值一次,为了说明这一点,需要在struct fibonacci中加入一个实例化时能产生warning的结构,还没想到。只好从编译速度上说明了。递归地算fibonacci(46)大概是快不了的。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #9 (permalink)  
旧 2004-07-11
高级会员
 
注册日期: 2002-09-19
帖子: 840
文章: 7
tomato 正向着好的方向发展
默认

引用:
作者: ajoo
对了。前段时间看见一篇用模板做nondeterministic程序的,很有趣。也许可以用gp做静态的24点哦。
不过动态编程只怕就不行了。
nondeterministic程序是指什么?
和nondeterministic Turing machine有关系么?

to ilovecpp
这不明摆着拿编译器当计算器使么?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #10 (permalink)  
旧 2004-07-12
高级会员
 
注册日期: 2002-09-15
帖子: 2,531
ajoo 正向着好的方向发展
默认

tomato: nondeterministic就是给定一个输入,输出可能不只一个。backtracking算法(穷举),就算是nondeterministic。

其实静态计算本来就是拿编译器当计算器。可行,有时有用,but no fun。

蹩脚的语法,无穷要关心的各种旮旯里的细节,天书一样的错误信息,让写静态计算成为一种折磨。

象问答里面出的那个判断函数类型,c++对这种很直观的问题给出了最晦涩的解答,要我说:靠!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #11 (permalink)  
旧 2004-07-12
普通会员
 
注册日期: 2003-09-10
住址: 重庆
帖子: 35
merlinran 正向着好的方向发展
发送 MSN 消息给 merlinran
默认

引用:
作者: ajoo
对了。前段时间看见一篇用模板做nondeterministic程序的,很有趣。也许可以用gp做静态的24点哦。
不过动态编程只怕就不行了。
那个nondeterministic程序,我猜ajoo大概是在这里看到的。这篇文章偶看了一年有余(从它刚刚写出来开始),恁没看懂(里面说的理论倒是一知半解,不过正应了文首所言:“本文很长,没几个人有耐心读完!”我就从来没看完过)。我所接触的人们似乎也没有一人说看懂过。看懂这篇文章似乎成了我的一个奋斗目标了

这里高手如云,我倒是想听听高见。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #12 (permalink)  
旧 2004-07-13
高级会员
 
注册日期: 2003-11-11
帖子: 147
ilovecpp 正向着好的方向发展
默认

引用:
作者: ajoo
其实静态计算本来就是拿编译器当计算器。可行,有时有用,but no fun。

蹩脚的语法,无穷要关心的各种旮旯里的细节,天书一样的错误信息,让写静态计算成为一种折磨。

象问答里面出的那个判断函数类型,c++对这种很直观的问题给出了最晦涩的解答,要我说:靠!
换个语言,可能对很直观的问题根本得不出任何解答,连 靠 都没得说。怎么用简单的方式给核心语言一点点可扩展性,还真没看到什么好的解答,除了完美的lisp。靠。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #13 (permalink)  
旧 2004-07-13
高级会员
 
注册日期: 2002-09-15
帖子: 2,531
ajoo 正向着好的方向发展
默认

那个文章真是他妈的有趣。说实话,我也没看完。
不过你这么一说,我仔细看了一下,越看越有意思。还没看完。不过第一次看模板程序看得这么爽的。

对了,咱们都仔细学习一下,组织一个关于这个文章的讨论如何?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #14 (permalink)  
旧 2004-07-17
高级会员
 
注册日期: 2004-02-20
帖子: 228
SnowFlacon 正向着好的方向发展
默认

这东西,用javascript,最简单。
排列组合所有的算式,然后eval就行了。错误的算式,例如x-+4518这种抓住exception 就行了。eval结果以后,筛掉同构的算式即可。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #15 (permalink)  
旧 2004-07-17
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,138
文章: 20
polyrandom 正向着好的方向发展
默认

引用:
作者: SnowFlacon
这东西,用javascript,最简单。
排列组合所有的算式,然后eval就行了。错误的算式,例如x-+4518这种抓住exception 就行了。eval结果以后,筛掉同构的算式即可。
最后一步在javascript里面事先不简单吧。
在C++中也可以用同样的方法,唯一的区别是要自己写一个eval。但是之所以C++的实现比较难是因为如果你在js中用这个方法,没人会说什么。如果你在C++里面用这样粗鲁的穷举,估计有人会由点想法的。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #16 (permalink)  
旧 2004-07-18
高级会员
 
注册日期: 2004-02-20
帖子: 228
SnowFlacon 正向着好的方向发展
默认

引用:
作者: PolyRandom
引用:
作者: SnowFlacon
这东西,用javascript,最简单。
排列组合所有的算式,然后eval就行了。错误的算式,例如x-+4518这种抓住exception 就行了。eval结果以后,筛掉同构的算式即可。
最后一步在javascript里面事先不简单吧。
在C++中也可以用同样的方法,唯一的区别是要自己写一个eval。但是之所以C++的实现比较难是因为如果你在js中用这个方法,没人会说什么。如果你在C++里面用这样粗鲁的穷举,估计有人会由点想法的。
其实一个小小的eval,就外显了巨大的区别。Program is data....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 09:56 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0