小学数学问题,居然没有什么思路,请指教

tangshuiling 2007-08-27 09:37:33
有四个数都是4(四个数都要用到),用四种方法+,-,*,/(当然括号还是可以用的)算出0到9,输出格式:(4+4)/4+4=6, 请指教勿见笑
...全文
1035 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
食人族哲学家 2007-09-05
  • 打赏
  • 举报
回复
后缀法好象和括号差不多,到了实现不过就是搞一下运算顺序,机器可不管先乘除,
sniperhuangwei 2007-08-29
  • 打赏
  • 举报
回复
用后缀法不就避免括号了.
sygb222 2007-08-29
  • 打赏
  • 举报
回复
既然是小学题目 应该是
(4-4) + (4-4) = 0
(4+4) / (4+4) = 1
(4/4) + (4/4) = 2

(4*4 -4) /4 = 3

(4-4)/4 +4 = 4
(4*4+4)/4 = 5
(4+4) / 4+4 = 6

(4-4/4)+4 = 7
4-4+4+4 = 8
4+4+4/4 = 9
食人族哲学家 2007-08-29
  • 打赏
  • 举报
回复
赞成楼上的next163(U鱼) ,不过每产生一个排列,就要算4种加括号的组合(没有括号的的1种和1组的3种2组括号的1种括号中还有括号的4种).这样比三曾循环好.代码绝对短得多.
stecdeng 2007-08-28
  • 打赏
  • 举报
回复
我就穷举
zz19910504 2007-08-28
  • 打赏
  • 举报
回复
可以用回溯

每次取2个数进行运算,在把结果当作下一层的运算数即可
tangshuiling 2007-08-28
  • 打赏
  • 举报
回复
jixingzhong(瞌睡虫·星辰)
在哪里有的看??能给个链接吗,十分感谢
jixingzhong 2007-08-28
  • 打赏
  • 举报
回复
看看 24 点计算的程序
next163 2007-08-28
  • 打赏
  • 举报
回复
不要用三层for循环,把四个符号取三个做全排列。然后插入4到各位置,然后再插入(),计算值。
antimatterworld 2007-08-28
  • 打赏
  • 举报
回复
线性规划!
tangshuiling 2007-08-28
  • 打赏
  • 举报
回复
肯定有意义啦~~能自己独立做出来,c++基本语法就掌握的差不多了!
jhs1982419 2007-08-28
  • 打赏
  • 举报
回复
这个是有点意思,不过做出来有什么作用呢 ??
sniperhuangwei 2007-08-28
  • 打赏
  • 举报
回复
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;
}
-----------------------------
这有个算24的
alps_008 2007-08-28
  • 打赏
  • 举报
回复
加减乘除可以用宏么?没这么用过,不知道行不行?比如
#define sy[0] +
#define sy[1] -
#define sy[2] *
#define sy[3] /
然后就是3层for来穷举

但是括号的位置不固定,比较不好弄
tangshuiling 2007-08-27
  • 打赏
  • 举报
回复
手算就免了~~还是要用c++的,否则问题也不会有意义
iatsbg 2007-08-27
  • 打赏
  • 举报
回复
穷举+-*/和不同的运算顺序啊~
手算也行。

64,684

社区成员

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

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