一个小学三年级的算术问题:编程求运用任意四则运算法使四个(1-99)的正整数解只为1的所有算式题。(100分)

X

ximyma

Unregistered / Unconfirmed
GUEST, unregistred user!
一个小学三年级的算术问题:编程求运用任意四则运算法使四个(1-99)的正整数解只为1的所有算式题。
我女儿上小学五年级,数学老师给假期的学生出了一个智力创新题,看哪位同学出的算术题最多,要求是:用四个1-99的正整数,任意运用四则运算+-*/,使最后得数为1。如1/1/1/1、10-7-1-1……,女儿想,老爸会不编程,这个第一肯定要拿,因为编程求出所有的解,抄录下来后,有谁更多?
老爸我想破了头,现在还是交白卷,倒不如老老实实一个一个的举例,写得还多一些。

大富翁们,是你们尽一个做父亲责任的时候了,快帮我设计好这个程序吧,别让我丢脸。
 
有趣,
观注
 
运算符号无法解决。无法实现。高手上。
 
靠,女儿是你的呀!!!,叫我们尽责任过分了点吧^_^
如果不考虑优先级的话OK了,VeryEasy,不过估计有几十亿个结果。估计你女儿要抄到大学毕业^_^
留下E-Mail把测试程序发给你
unit Unit1;
interface
uses
Windows,Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
bStop : Boolean;
public
{ Public declarations }
function Calc(m,n :do
uble;
s : Integer) :do
uble;
procedure IsOK(i1,i2,i3,i4,s1,s2,s3 : Integer);
end;

var
Form1: TForm1;
implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
I1,i2,i3,i4 : Integer;
s1,s2,s3 : Integer;
begin
bStop := False;;
for I1 := 1 to 99do
begin
for s1 := 1 to 4do
begin
for i2 := 1 to 99do
begin
for s2 := 1 to 4do
begin
for i3 := 1 to 99do
begin
for s3 :=1 to 4do
begin
for i4 := 1 to 99do
begin
if bStop then
Exit;
IsOK(i1,i2,i3,i4,s1,s2,s3);
Application.ProcessMessages;
end;
end;
end;
end;
end;
end
end;
end;

function TForm1.Calc(m,n :do
uble;
s : Integer) :do
uble;
begin
result := 0;
case s of
1 : Result := m + n;
2 : Result := m - n;
3 : Result := m * n;
4 : Result := m / n;
end;
end;

procedure TForm1.IsOK(i1, i2, i3, i4, s1, s2, s3: Integer);
var
r :do
uble;
S : String;
begin
R := Calc(i1,i2,s1);
if (0 <> (R - Int(R))) then
Exit;
// 去掉浮点数
R := Calc(R,i3,s2);
if (0 <> (R - Int(R))) then
Exit;
// 去掉浮点数
R := Calc(R,i4,s3);
if (1 <> R ) then
Exit;
S := '+-*/';
Memo1.Lines.Append(IntToStr(i1) + S[s1] + IntToStr(i2) + S[s2] + IntToStr(i3) + S[s3] + IntToStr(i4));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
bStop := True;
end;

end.

 
以下代码已经调试通过,应该能够解决你的问题
(输出的结果计算顺序为从左到右,而不是按照四则运算的优先级,如 1+1*47/94=1)
procedure TForm1.Button1Click(Sender: TObject);
var i1,i2,i3,i4,s1,s2,s3:integer;
str:string;
function ResultOf(ss:string):integer;
var ch:string;
i,j:integer;
sz:array[1..4] of integer;
ysf:array[1..3] of byte;
begin
i:=1;
for j:=1 to 4do
begin
ch:='';
while ss in ['0'..'9']do
begin
ch:=ch+ss;
i:=i+1;
end;
sz[j]:=strtoint(ch);
if j<4 then
ysf[j]:=ord(ss);
i:=i+1;
end;
case ysf[1] of
43:i:=sz[1]+sz[2];
45:i:=sz[1]-sz[2];
42:i:=sz[1]*sz[2];
47:if sz[1] mod sz[2] =0 then
i:=sz[1] div sz[3]
else
begin
result:=0;exit;
end;
end;
case ysf[2] of
43:i:=i+sz[3];
45:i:=i-sz[3];
42:i:=i*sz[3];
47:if i mod sz[3] =0 then
i:=i div sz[3]
else
begin
result:=0;exit;
end;
end;
case ysf[3] of
43:i:=i+sz[4];
45:i:=i-sz[4];
42:i:=i*sz[4];
47:if i mod sz[4] =0 then
i:=i div sz[4]
else
begin
result:=0;exit;
end;
end;
result:=i;
end;

begin
for i1:=1 to 99do
for s1:=1 to 4do
for i2:=1 to 99do
for s2:=1 to 4do
for i3:=1 to 99do
for s3:=1 to 4do
for i4:=1 to 99do
begin
case s1 of
1:str:=inttostr(i1)+'+';
2:str:=inttostr(i1)+'-';
3:str:=inttostr(i1)+'*';
4:str:=inttostr(i1)+'/';
end;

case s2 of
1:str:=str+inttostr(i2)+'+';
2:str:=str+inttostr(i2)+'-';
3:str:=str+inttostr(i2)+'*';
4:str:=str+inttostr(i2)+'/';
end;

case s3 of
1:str:=str+inttostr(i3)+'+';
2:str:=str+inttostr(i3)+'-';
3:str:=str+inttostr(i3)+'*';
4:str:=str+inttostr(i3)+'/';
end;
str:=str+inttostr(i4);
edit1.Text :=str;
edit1.Update ;
if ResultOf(str)=1 then
memo1.lines.add(str);
end;

end;
 
mailto:xm8088@163.com
 
这个是考虑优先级的算法,乘法和除法优先级比加法和减法高。
unit Unit1;
interface
uses
Windows,Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
bStop : Boolean;
public
{ Public declarations }
function Calc(m,n :do
uble;
s : Integer) :do
uble;
procedure IsOK(i1,i2,i3,i4,s1,s2,s3 : Integer);
end;

var
Form1: TForm1;
implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
I1,i2,i3,i4 : Integer;
s1,s2,s3 : Integer;
begin
bStop := False;;
for I1 := 1 to 99do
begin
for s1 := 1 to 4do
begin
for i2 := 1 to 99do
begin
for s2 := 1 to 4do
begin
for i3 := 1 to 99do
begin
for s3 :=1 to 4do
begin
for i4 := 1 to 99do
begin
if bStop then
Exit;
IsOK(i1,i2,i3,i4,s1,s2,s3);
Application.ProcessMessages;
end;
end;
end;
end;
end;
end
end;
end;

function TForm1.Calc(m,n :do
uble;
s : Integer) :do
uble;
begin
result := 0;
case s of
1 : Result := m + n;
2 : Result := m - n;
3 : Result := m * n;
4 : Result := m / n;
end;
end;

procedure TForm1.IsOK(i1, i2, i3, i4, s1, s2, s3: Integer);
var
r :do
uble;
S : String;
L : Integer;
begin
L := (s1 + s1 mod 2) div 2 * 100 + (s2 + s2 mod 2) div 2 * 10 + (s3 + s3 mod 2) div 2;
case L of
111,211,221,222 : begin
R := Calc(i1,i2,s1);
R := Calc(R,i3,s2);
R := Calc(R,i4,s3);
end;

112,212 : begin
R := Calc(Calc(i1,i2,s1),Calc(i3,i4,s3),s2);
end;

121 : begin
R := Calc(i2,i3 ,s2);
R := Calc(R ,i1 ,s1);
R := Calc(R ,i4 ,s3);
end;

122 : begin
R := Calc(i2,i3 ,s2);
R := Calc(R ,i4 ,s3);
R := Calc(R ,i1 ,s1);
end;
else
Memo1.Lines.Append('不可能(' + IntToStr(L) + ')');
R := 1;
end;

if (1 <> R ) then
Exit;
S := '+-*/';
Memo1.Lines.Append(IntToStr(i1) + S[s1] + IntToStr(i2) + S[s2] + IntToStr(i3) + S[s3] + IntToStr(i4));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
bStop := True;
end;

end.
 
找一个24点的程序代码,把24改成1就行了,
以前写过类似的程序,能处理8,8,8,3,很多24点程序不能处理.
可惜代码不见了
 
以下是输出结果片断,结果大大减少,没有几十亿了,呵呵^_^,高中以前应该能抄完,不过你女儿的数学老师挺黑的.....
1+1-20/20
1+1-21+20
1+1-21/21
1+1-22+21
1+1-22/22
1+1-23+22
1+1-23/23
1+1-24+23
1+1-24/24
1+1-25+24
1+1-25/25
1+1-26+25
1+8-63+55
1+8-64+56
1+8-64/8
1+8-65+57
1+8-66+58
 
感谢大家的帮忙。loxtln的程序我有点看不懂,不过已经调试通过,只是memo要加一个SCOLLBAR,还有,到现在它还在执行,不知道要什么时候才能全部结束。我该全部都打印出来吗?还是把这个程序送给数学老师?
大家没意见的话,loxtln得80分行不行?
 
ximyma, 兄 程序已经发到你的信箱里面了,请打分,看能不能得满分? ^_^
 
要不del给80吧?
 
吐血,偏心的家伙,我第一个回答,答案又最好,我的还有停止 按钮 呢,而且又能看懂,只留 20 分给我,呜呜呜呜!!!
 
ximyma, 兄,要是你女儿写的程序就可以给老师了,否则不是露馅了吗????
 
啊呀,
穆龙先生的程式真的很棒啊,
我给穆龙先生80分吧?
loxtln有意见吗?
 
我仔细的看了两个程序的执行结果,从准确率来说,穆龙先生的百分之百准确,LOXTLN先生的程序有错误的算式。从速度上来说呢,穆龙先生先走一步,占得先机,但是……
穆龙先生的程序很好,只是执行到1+10+N*(/)N时,就停止了,是不是MEMO的容量到头了,还是程序出现了异常?
另外,我搞不清楚两位使用了什么算法,能不能给咱们说说,看看谁的原理更先进?
或者谁能做得更好一点,分就多分一点给他?
今天要睡觉了,不玩了,明天上来看看!
 
吓我一跳,以为程序错了,自己测试了一下,跑了几分种,下面是结果,推断你用的是win98 ,Win98 下 Memo 只能承受 64K,另外,我的算法和Loxtln兄的算法一样,没有什么不一样的,都是一堆循环。只是Loxtln兄用字符串转来转去,麻烦了些。
你什么地方不明白直说吧。
2+48-1-48
2+48-1*49
2+48-2-47
2+48-3-46
2+48-4-45
2+48-5-44
2+48-6-43
2+48-7-42
2+48-7*7
2+48-8-41
2+48-9-40
2+48-10-39
2+48-11-38
2+48-12-37
2+48-13-36
2+48-14-35
2+48-15-34
2+48-16-33
2+48-17-32
 
算法,你看一下我的第一个,没有考虑优先级的那个,IsOK 判断是否 = 1 等于 1 就加入 Memo......
 
我试了一下
"运用任意四则[red]混合[/red]运算法使四个[red](1-2)[/red]的正整数解只为1的所有算式题"
(1+(1-(1*1)))=1
(1+(1-(1/1)))=1
(1+(1*(1-1)))=1
(1-(1-(1*1)))=1
(1-(1-(1/1)))=1
(1-(1*(1-1)))=1
(1*(1+(1-1)))=1
(1*(1-(1-1)))=1
(1*(1*(1*1)))=1
(1*(1*(1/1)))=1
(1*(1/(1*1)))=1
(1*(1/(1/1)))=1
(1/(1+(1-1)))=1
(1/(1-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)))=1
(2*(1/(2/1)))=1
(2/(1+(2-1)))=1
(2/(1*(2*1)))=1
(2/(1*(2/1)))=1
(2+(1-(1*2)))=1
(2+(1*(1-2)))=1
(2+(1/(1-2)))=1
(2*(1-(1/2)))=1
(2*(1*(1/2)))=1
(2*(1/(1*2)))=1
(2/(1-(1-2)))=1
(2/(1*(1*2)))=1
(2/(1/(1/2)))=1
(2-(2-(1*1)))=1
(2-(2-(1/1)))=1
(2-(2/(1+1)))=1
(2/(2+(1-1)))=1
(2/(2-(1-1)))=1
(2/(2*(1*1)))=1
(2/(2*(1/1)))=1
(2/(2/(1*1)))=1
(2/(2/(1/1)))=1
(1+(2*(2-2)))=1
(1-(2*(2-2)))=1
(1*(2-(2/2)))=1
(1/(2-(2/2)))=1
(2-(1+(2-2)))=1
(2-(1-(2-2)))=1
(2-(1*(2/2)))=1
(2-(1/(2/2)))=1
(2/(1+(2/2)))=1
(2+(2-(1+2)))=1
(2-(2+(1-2)))=1
(2-(2*(1/2)))=1
(2-(2/(1*2)))=1
(2+(2-(2+1)))=1
(2-(2-(2-1)))=1
(2-(2/(2*1)))=1
(2-(2/(2/1)))=1
(2/(2*(2-1)))=1
(2/(2/(2-1)))=1
(2-(2-(2/2)))=1
(2*(2/(2+2)))=1
(2*(2/(2*2)))=1
(2/(2+(2-2)))=1
(2/(2-(2-2)))=1
(2/(2*(2/2)))=1
(2/(2/(2/2)))=1
 
看来穆龙兄的程序要好一些,
运行的速度很快,
就是我的瘟酒吧……
 
顶部