四個數的算數運算

shine2000 2005-09-02 10:44:26
任意輸入0-9之間的四個數(不能重復),然後對四個數進行+ - * / 任意運算,如果運算結果在1-49之間就輸出。不知道有什麼好的算法沒
...全文
254 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zwl327 2005-09-05
  • 打赏
  • 举报
回复
用遍历,不过剪枝函数上可以研究一下,以提高效率,
packe_peng_520 2005-09-05
  • 打赏
  • 举报
回复
up
peterx_xin 2005-09-05
  • 打赏
  • 举报
回复
有点难………………
顶一下!
希望之晨 2005-09-04
  • 打赏
  • 举报
回复
有点难,帮你定下.
shine2000 2005-09-04
  • 打赏
  • 举报
回复
up
hoya5121 2005-09-03
  • 打赏
  • 举报
回复
记得好像用树比较方便,把数字和符号压到树里。。。记不清楚了。。
shine2000 2005-09-03
  • 打赏
  • 举报
回复
不用加括號啊,需要考慮運算符號的優先集
shine2000 2005-09-03
  • 打赏
  • 举报
回复
沒人幫忙看下嗎,具體點啊
shine2000 2005-09-03
  • 打赏
  • 举报
回复
那個寫個代碼看下啊
mituzhishi 2005-09-03
  • 打赏
  • 举报
回复
当然是穷举法了
shine2000 2005-09-03
  • 打赏
  • 举报
回复
有人給個詳細點的算法嗎
wzjall 2005-09-02
  • 打赏
  • 举报
回复
自己改改吧
我的可以计算"任意"的四则运算. +-*/()
你的当然也能行
wzjall 2005-09-02
  • 打赏
  • 举报
回复
#pragma warning(disable:4786)
#include<string>
#include<map>
#include<stack>
#include<iostream>
#include<ctype.h>
using namespace std;
void main()
{
map<string, char>table;
table["++"]='>';
table["+-"]='>';
table["+*"]='<';
table["+/"]='<';
table["+("]='<';
table["+)"]='>';
table["+#"]='>';
table["-+"]='>';
table["--"]='>';
table["-*"]='<';
table["-/"]='<';
table["-("]='<';
table["-)"]='>';
table["-#"]='>';
table["*+"]='>';
table["*-"]='>';
table["**"]='>';
table["*/"]='>';
table["*("]='<';
table["*)"]='>';
table["*#"]='>';
table["/+"]='>';
table["/-"]='>';
table["/*"]='>';
table["//"]='>';
table["/("]='<';
table["/)"]='>';
table["/#"]='>';
table[")+"]='>';
table[")-"]='>';
table[")*"]='>';
table[")/"]='>';
table["))"]='>';
table[")#"]='>';
table["(+"]='<';
table["(-"]='<';
table["(*"]='<';
table["(/"]='<';
table["(("]='<';
table["()"]='=';
table["#+"]='<';
table["#-"]='<';
table["#*"]='<';
table["#/"]='<';
table["#("]='<';
table["##"]='=';


stack<float> num;
stack<char> alp;
alp.push('#');



string s="35*10*50";
s+='#';
int i=0;
char c=s[i++];
int tag=0;



while(c!='#'||alp.top()!='#')
{
if(isdigit(c))
{
char d=s[i];
tag++;

if(isdigit(d))
{
num.push((float)c-48);
}
else
{
num.push((float)c-48);

float n=1,m=0;
for(int j=tag;j>0;j--)
{
m=num.top()*n+m;
n=n*10;
num.pop();
}
num.push(m);
tag=0;
}
//num.push((float)c-48);//字符转数字
c=s[i++];
}
else
{

string s1;
s1+=alp.top();
s1+=c;
char c1=table[s1];


switch(c1)
{
case'<':
alp.push(c);
c=s[i++];
break;
case'=':
alp.pop();
c=s[i++];
break;
case'>':
float y=num.top();
num.pop();
float x=num.top();
num.pop();
char c2=alp.top();
alp.pop();
switch(c2)
{
case'+':
x=x+y;
break;
case'-':
x=x-y;
break;
case'*':
x=x*y;
break;
case'/':
x=x/y;
break;
}
num.push(x);

cout<<num.top()<<endl;

break;

}
}

}
cout<<num.top();
}
myhouwei 2005-09-02
  • 打赏
  • 举报
回复
需考虑优先级不,比如先乘除后加减,或则可以人为加括号不?
xiaocaizi 2005-09-02
  • 打赏
  • 举报
回复
穷举的办法
用个四元组表示四个运算符号
(op1,op2,op3,op4)
op1...op2..op3..op4都可以取+ -* / 算出每种组合的结果就可以了
共有256种情况
shine2000 2005-09-02
  • 打赏
  • 举报
回复
不是啊 就是任意輸入四個數  比如  3 5 7 9 就像玩24點一樣啊 就這4個數做運算就好了
3+5+7+9 = 24  結果1<= 24 <= 49 輸出  3 +5* 7/ 9 = ........
snowbirdfly 2005-09-02
  • 打赏
  • 举报
回复
应该算吧~
这样就情况比较多~~
想一下~
先顶一下~~
xiaocaizi 2005-09-02
  • 打赏
  • 举报
回复
假如输入的是1,2,3,4

1)这四个数字是不是一定要用上?
比如 1+2=3 算吗
2)这四个数字可否重复用?
比如
1+2+3+4+4+3+2+1=20 算吗
SUPERSYB 2005-09-02
  • 打赏
  • 举报
回复
用回逆法,只有四个数用递归也来得快!!

65,210

社区成员

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

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