编程:5个5,算出24来,只用加减乘除法算(允许用括号)

davidlcz 2007-04-23 05:48:37
编程:5个5,算出24来,只用加减乘除法算(允许用括号)

(从百度里搜索到答案)

使用加减乘除和乘方、括号,全解有7个:

(5-5/(5*5))*5=24
(5-5/5/5)*5=24
5*(5-5/(5*5))=24
5*(5-5/5/5)=24
5*5-(5/5)^5=24
5*5-5^(5-5)=24
(5*5*5-5)/5=24

可不知道如何写程序输出所有结果,请求高手帮忙
...全文
1182 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sniperhuangwei 2007-04-25
  • 打赏
  • 举报
回复
stack.h
-------------------------------------------------------------------
#include <vector>


class stack
{
public:

void push(float arg)
{
data.push_back(arg);
}

float pop()
{

std::vector<float>::iterator it = data.end();
--it;
float result = *it;
data.erase(it);
return result;
}

int size()
{
return data.size();
}

void clear()
{
data.clear();
}

private:
std::vector<float> data;
};
-------------------------------
24.cpp
-------------------------------------------------------------------------
#include <stdio.h>
#include "stack.h"
int size;
int count;
int total;
static int opnum = 0;
static int arraynum = 0;
std::vector<int*> shuzu;
std::vector<char*> caozuo;
stack Stack;
void pushToArray(int *temarray,int *shu,char *cao,int numshu,int numcao);
void cal(int *temp);

void swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}

char op[4] = {'+','-','*','/'};

void pailie(int *array,int n)
{
static int *a = array;
if( 1 == n )
{
++arraynum;
int *temp = array-size+1;
int *temp1 = (int *)malloc(sizeof(int)*size);
memcpy(temp1,temp,sizeof(int)*size);
shuzu.push_back(temp1);
}
else
{
pailie(array+1,n-1);
for( int i = 1; i < n; ++i)
{
if(array[0] == array[i] || a[0] == array[i] ||
(a[0] == array[0] && n != size))
continue;
swap(array[0],array[i]);
pailie(array+1,n-1);
swap(array[i],array[0]);
}
}
}

void operate(char *target,int n)
{
if( 0 == n)
{
//char *temp = target - count;
++opnum;
--target;
char *temp = (char*)malloc(sizeof(char)*count);
for(int i = count-1; i >= 0; --i,--target)
{
temp[i] = *target;
}
caozuo.push_back(temp);
}
else
{
for( int i = 0; i < 4; ++i)
{
*target = op[i];

operate(target+1,n-1);
}

}
}

int loop()
{
std::vector<int *>::iterator it = shuzu.begin();
std::vector<int *>::iterator end = shuzu.end();
int *oparray = (int *)malloc(sizeof(int)*(size+count));
for( ; it != end; ++it)
{
int *temarray = *it;

oparray[0] = temarray[0];
oparray[1] = temarray[1];

std::vector<char *>::iterator it1 = caozuo.begin();
std::vector<char *>::iterator end1 = caozuo.end();
for( ; it1 != end1; ++it1)
{
pushToArray(oparray+2,temarray+2,*it1,2,0);
}

}

}

void pushToArray(int *temarray,int *shu,char *cao,int numshu,int numcao)
{
if(numcao == count)
{
int *temp = temarray - (size + count);
cal(temp);
}
else
{
if(numshu == size)
{
*temarray = (int)*cao;
pushToArray(temarray+1,shu,cao+1,numshu,numcao+1);
}else
{
*temarray = *shu;
pushToArray(temarray+1,shu+1,cao,numshu+1,numcao);
if(numcao + 1 < numshu)
{
*temarray = (int)*cao;
pushToArray(temarray+1,shu,cao+1,numshu,numcao+1);
}
}
}
}

void cal(int *temp)
{
for(int i = 0 ; i < size + count; ++i)
{
switch(temp[i])
{
case '+':
{
float a = Stack.pop();
float b = Stack.pop();
float c = a + b;
Stack.push(c);
}
break;
case '-':
{
float a = Stack.pop();
float b = Stack.pop();
float c = b - a;
Stack.push(c);
}
break;
case '*':
{
float a = Stack.pop();
float b = Stack.pop();
float c = a * b;
Stack.push(c);
}
break;
case '/':
{
float a = Stack.pop();
float b = Stack.pop();
if( a == 0)
{
Stack.clear();
return;
}
float c = b / a;
Stack.push(c);
}
break;
default:
Stack.push((float)temp[i]);
break;
}

}
float result = Stack.pop();
if(result == (float)total)
{
for(int i = 0; i < size + count; ++i)
{
char op = (char)temp[i];
if( op == '+' || op == '-' || op == '*' || op == '/')
printf(" %c",op);
else
printf(" %d",temp[i]);
}
printf("\n");
}
}


int main()
{
printf("please input size:");
scanf("%d",&size);
count = size - 1;
int *array = (int*)malloc(sizeof(*array)*size);
char *array2 = (char*)malloc(sizeof(*array)*count);
printf("please input %d number:\n",size);
for(int i = 0; i < size; ++i)
{
printf("input %d :",i+1);
scanf("%d",&array[i]);
}
printf("please input total:");
scanf("%d",&total);
pailie(array,size);
operate(array2,count);
loop();
return 0;
}
-----------------------------
随便你算几,输出是后缀表示法
lzp729 2007-04-24
  • 打赏
  • 举报
回复
最简单的问题往往需要最高深的理论,比如歌德巴赫-_-
这个问题估计涉及到高级AI了吧
jixingzhong 2007-04-24
  • 打赏
  • 举报
回复
#include <stdio.h>
typedef float (__cdecl *TYPE_MYFUN)(float , float );
float FunAdd(float x, float y)
{
return x + y;
}
float FunSub(float x, float y)
{
return (x - y>0)?x-y:y-x;
}

float FunMul(float x, float y)
{
return x * y;
}
float FunDiv(float x, float y)
{
return x / y;
}
bool operatorFun( float a, float b ,float c ,float d);


int main(int argc, char* argv[])
{
//float Num[4] = { 3 , 3 , 8 , 8 };//答案为 8/(3-8/3) ;
float Num[4] = { 5 , 5 , 5 , 5 };
for( int a = 0 ; a < 4 ; a++ ){
for( int b = 0 ; b < 4 ;b++ ){
for( int c = 0 ; c < 4 ; c++ ){
for( int d = 0 ; d < 4 ; d++ ){
if((a==b)||(a==c)||(a==d)||(b==c)||(b==d)||(c==d))
continue;
if ( operatorFun(Num[a],Num[b],Num[c],Num[d]) )
return 0;
}
}
}
}
return 0;
}


bool operatorFun( float a, float b ,float c ,float d)
{
static TYPE_MYFUN Fun[4] = { FunAdd ,FunSub , FunMul , FunDiv };
static char op[4] = { '+' , '-' , '*' ,'/'};

for( int x = 0 ; x < 4 ; x++ ){
for( int y = 0 ; y < 4 ; y++ ){
for( int z = 0 ; z < 4 ; z++ ){
float sum = Fun[z]( Fun[y]( Fun[x](a,b) ,c) ,d);
if( (sum >23.9)&&(sum<24.1) ){
printf("((%d%c%d)%c%d)%c%d",(int)a,op[x] ,(int)b,op[y] ,(int)c,op[z],(int)d);
return true;
}
sum = Fun[z]( Fun[x](a,b) , Fun[y](c ,d) );
if( (sum >23.9)&&(sum<24.1) ){
printf("(%d%c%d)%c(%d%c%d)",(int)a,op[x] ,(int)b,op[z] ,(int)c,op[y],(int)d);
return true;
}
sum = Fun[z]( a , Fun[y](b , Fun[x](c,d) ) );
if( (sum >23.9)&&(sum<24.1) ){
printf("%d%c(%d%c(%d%c%d))",(int)a,op[z] ,(int)b,op[y] ,(int)c,op[x],(int)d);
return true;
}
}
}
}
return false;
}

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1444224
thinkinnight 2007-04-24
  • 打赏
  • 举报
回复
(5-1/5)*5
然后5/5=1
taodm 2007-04-24
  • 打赏
  • 举报
回复
穷举法。
ppcat_001 2007-04-24
  • 打赏
  • 举报
回复
5*5-(5/5)^5=24
5*5-5^(5-5)=24
允许用乘方?
proxiaobai 2007-04-24
  • 打赏
  • 举报
回复
深度遍历求
thinkinnight 2007-04-24
  • 打赏
  • 举报
回复
对了,还要除去重复的解,看来还不是那么简单
thinkinnight 2007-04-24
  • 打赏
  • 举报
回复
要计算出这个的话,将上面的程序稍微改一下就可以了,让main中的循环走到最后,同时要改为适合5个数的
lidongri 2007-04-23
  • 打赏
  • 举报
回复
穷举
  • 打赏
  • 举报
回复
和那个4个数算24点很类似的,我用的是穷举方法.

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧