编程实现"小学二年级暑期作业智力题"(200分)

M

maming

Unregistered / Unconfirmed
GUEST, unregistred user!
用0,1,2,3,4,5,6,7,8,9 十个数,组成一个等式(两个三位数相加等于一个四位数),
且每个数不能重复使用。如: xxx+xxx=xxxx
手工推算的话是可以,但要得出所有解的话也不简单。所以想程序实现,大家各显身手吧!
有分又好玩。
 
没什么难度,笨的方法就多用几次循环
聪明的方法就用数类的概念,不就ok了
 
写出来看看,看谁的方法更好。
 
>>每个数不能重复使用
什么意思?是指所用到的三位数吗?
 
如按上理解,符合要求共448个算式。
 
程序。要有程序,或是算法。
 
我来贴一个笨一点的方法给你看一下!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
ta=array[1..10] of integer;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
function istrue(a:ta):boolean;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation

{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var a:ta;
i,j,s,k:integer;
begin
for i:=102 to 987 do
begin
a[1]:=i div 100;
//beep;
a[2]:=(i-a[1]*100) div 10;
a[3]:=i mod 10;
for j:=4 to 10 do
a[j]:=j-20;
if istrue(a) then
Continue;
for j:=102 to 987 do
begin
a[4]:=j div 100;
a[5]:=(j-a[4]*100) div 10;
a[6]:=j mod 10;
if istrue(a) then
continue
else
begin
s:=i+j;
//beep;
a[7]:=s div 1000;
a[8]:=s div 100 mod 10;
a[9]:=s div 10 mod 10;
a[10]:=s mod 10;
if istrue(a) then
begin
for k:=7 to 10 do
a[k]:=k-20;
continue;
end
else
listbox1.Items.Add(inttostr(i)+'+'+inttostr(j)+'='+inttostr(s));
end;

end;
end;
end;

function TForm1.istrue(a: ta): boolean;
var
i,j:integer;
begin
result:=False;
for i:=1 to 9 do
begin
for j:=i+1 to 10 do
if a=a[j] then
begin
result:=true;
exit;
end;
end;
end;

end.
 
刚才那个单击的过程中少了一句,
procedure TForm1.Button1Click(Sender: TObject);
.
.
s:=i+j;
a[7]:=s div 1000;
if a[7]=0 then
continue;
//上面的程序中还要添加这一句, 不好意思呀 :P
a[8]:=s div 100 mod 10;
a[9]:=s div 10 mod 10;
  .
.
.
 
最笨的办法就是穷举
 
题目没说清楚,作为结果的四位数可以用0开头吗?
 
procedure TForm1.Button1Click(Sender: TObject);
var
i, j, m, k,s: integer;
num:array[1..1000] of integer;
ok:boolean;
begin
k := 0;
RichEdit1.lines.clear;
i := 100;
j := 101;
s:=1;
m:=1;
Num[1]:=0;
while i < 999 do
begin
j := i + 1;
while j < 1000 do
begin
if (i + j > 999) then
begin
m:=1;
ok:=true;
while (m<=s) and ok do
begin
if (i=Num[m]) or (j=Num[m]) or (i=j) then
ok:=false;
inc(m);
end;
if ok then
begin
inc(k);
Num[s+1]:=i;
Num[s+2]:=j;
RichEdit1.lines.add(intToStr(i) + '+' + IntToStr(j) + '=' + IntToStr(i + j));
s:=s+2;
inc(i);
end;
end;
inc(j);
end;
inc(i);
end;
end;
 
1、当然不用0做开头;
2、我发现各位大虾的程序都不写注释的,这样,我等想学也还真不容易,个人认为应该
要有注释,这是个好习惯。
 
穷举是最好的方法。。。。
这种程序又不要求速度快。。。
还不浪费自己脑力
 
to linsb:你和程序不合要求,"每个数字不用重复使用:"。
张辉明的代码完全实现了。
还有那位大虾写出更优的代码。
 
to maming
>>每个数不能重复使用
>>什么意思?是指所用到的三位数吗?
>>如按上理解,符合要求共448个算式。
我发过两次贴,你都不表态,
你的题是有歧义的!
 
to linsb:是你理解错误,张辉明做出来了,是对的。
但张的代码还可以优化。仔细分析一下,
最小的三位数应该不是102,因为1xx+9xx是能成为四位数,但不符要求,应该从2xx开始
最大的三位数据也就不能是987了,所以只能到879。
因最大的只能是879,所以最小的也就不可能是比23x了(因为1和2都使用了)。
 
我把我的理解告诉给你(两次),但你为什么不指出,害了我费了脑筋?
 
随便写了一个,更得到48种组合
我没有加推理在里面,因为如果那样的话,干脆直接告诉电脑的了!
procedure ca1(var AMemo:Tmemo);
var i,j,k,m,n:integer;
strTemp:string;
strThr:array of String;
total:Integer;
intTemp:Integer;
CurTime:TDateTime;
ResTime:TDateTime;
begin
AMemo.Lines.Clear;
total:=0;
CurTime:=Time();
for i:=1 to 9 do
for j:=0 to 9 do
for k:=0 to 9 do
begin
if (i=j)or(j=k)or(i=k) then
continue;
Setlength(strThr,Length(strThr)+1);
strThr[high(strThr)]:=InttoStr(i)+InttoStr(j)+InttoStr(k);
end;
for i:=0 to high(strThr) do
for j:=i to high(strThr) do
begin
k:=strtoint(strThr)+strtoint(strThr[j]);
strTemp:=strThr+strThr[j]+inttostr(k);
if length(strTemp)<10 then
continue;
for n:=0 to 9 do
begin
intTemp:=0;
for m:=1 to length(strTemp) do
begin
if strTemp[m]=inttostr(n) then
inc(intTemp);
if intTemp>=2 then
break;
end;
if intTemp>=2 then
break;
end;
if intTemp>=2 then
continue;
if k>=1000 then
//两个三位数相加不可能大于四位数
begin
AMemo.lines.Add(strThr+'+'+strThr[j]+'='+inttostr(k));
inc(total);
end;
end;
ResTime:=Time();
AMemo.Lines.Add('更有'+inttostr(total)+'种组合,更耗时'+FormatDateTime('hh:nn:ss:zzz',ResTime-CurTime))
end;
 
to linsb:对不起,因为没有及时关注。
to only you:答案应该不是48种,总共是95种。
 
95种,不会吧!我得到的算式列出,请你举一个反例!
246+789=1035
249+786=1035
264+789=1053
269+784=1053
284+769=1053
286+749=1035
289+746=1035
289+764=1053
324+765=1089
325+764=1089
342+756=1098
346+752=1098
347+859=1206
349+857=1206
352+746=1098
356+742=1098
357+849=1206
359+847=1206
364+725=1089
365+724=1089
423+675=1098
425+673=1098
426+879=1305
429+876=1305
432+657=1089
437+589=1026
437+652=1089
439+587=1026
452+637=1089
457+632=1089
473+589=1062
473+625=1098
475+623=1098
476+829=1305
479+583=1062
479+826=1305
483+579=1062
487+539=1026
489+537=1026
489+573=1062
624+879=1503
629+874=1503
674+829=1503
679+824=1503
743+859=1602
749+853=1602
753+849=1602
759+843=1602
更有48种组合,更耗时00:00:04:307
 
顶部