pascal语言编程十万火急
有六箱货物,重分别是5吨、2吨、3.5吨、1.7吨、1吨、5.1吨。现有一台货车,载重量10吨。设计一个程序,使这次车运走的货物最多一、不要用real,用extended...
有六箱货物,重分别是5吨、2吨、3.5吨、1.7吨、1吨、5.1吨。现有一台货车,载重量10吨。设计一个程序,使这次车运走的货物最多
一、不要用real,用extended,这个数据类型也是可以计算小数的,而且结果比real的结果更精确。
二、这个程序还能优化吗? 展开
一、不要用real,用extended,这个数据类型也是可以计算小数的,而且结果比real的结果更精确。
二、这个程序还能优化吗? 展开
3个回答
展开全部
我用枚举法来解决你的问题,对每一张组合方法进行测试,把组合的结果小于10的最大数选出来。
枚举有两种方法,第一种用六重循环,每一个循环变量从0~1进行,1表示装载这一箱,0表示不装载,简单的程序代码如下:
var
max,sum:real;
a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6:boolean;
begin
max:=0;
for a1:=false to true do
for a2:=false to true do
for a3:=false to true do
for a4:=false to true do
for a5:=false to true do
for a6:=false to true do
begin
sum:=0;
if a1 then sum:=sum+5;
if a2 then sum:=sum+2;
if a3 then sum:=sum+3.5;
if a4 then sum:=sum+1.7;
if a5 then sum:=sum+1;
if a6 then sum:=sum+5.1;
if (sum<=10)and(sum>max) then
begin
max:=sum;
b1:=a1;
b2:=a2;
b3:=a3;
b4:=a4;
b5:=a5;
b6:=a6;
end;
end;
writeln('最多可以装',max:1:1,'吨,装以下货');
if b1 then write('5吨 ');
if b2 then write('2吨 ');
if b3 then write('35吨 ');
if b4 then write('1.7吨 ');
if b5 then write('1吨 ');
if b6 then write('5.1吨 ');
end.
运行结果如下:
最多可以装9.8吨,装以下货
2吨 1.7吨 1吨 5.1吨
第二种办法和第一种原理相同,但是对这六箱货是否装载进行二进制编码,结果一个0~63,这样可以用一重循环来实现,完整程序如下:
{$apptype console}
program temp;
var
a,b: integer;
max,sum:real;
begin
max:=0;
for a:=0 to 63 do
begin
sum:=0;
if a and 1<>0 then sum:=sum+5;
if a and 2<>0 then sum:=sum+2;
if a and 4<>0 then sum:=sum+3.5;
if a and 8<>0 then sum:=sum+1.7;
if a and 16<>0 then sum:=sum+1;
if a and 23<>0 then sum:=sum+5.1;
if (sum<=10)and(sum>max) then
begin
max:=sum;
b:=a;
end;
end;
writeln('最多可以装',max:1:1,'吨,装以下货');
if b and 1<>0 then write('5吨 ');
if b and 2<>0 then write('2吨 ');
if b and 4<>0 then write('35吨 ');
if b and 8<>0 then write('1.7吨 ');
if b and 16<>0 then write('1吨 ');
if b and 32<>0 then write('5.1吨 ');
end.
运行结果同上,即:
最多可以装9.8吨,装以下货
2吨 1.7吨 1吨
枚举有两种方法,第一种用六重循环,每一个循环变量从0~1进行,1表示装载这一箱,0表示不装载,简单的程序代码如下:
var
max,sum:real;
a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6:boolean;
begin
max:=0;
for a1:=false to true do
for a2:=false to true do
for a3:=false to true do
for a4:=false to true do
for a5:=false to true do
for a6:=false to true do
begin
sum:=0;
if a1 then sum:=sum+5;
if a2 then sum:=sum+2;
if a3 then sum:=sum+3.5;
if a4 then sum:=sum+1.7;
if a5 then sum:=sum+1;
if a6 then sum:=sum+5.1;
if (sum<=10)and(sum>max) then
begin
max:=sum;
b1:=a1;
b2:=a2;
b3:=a3;
b4:=a4;
b5:=a5;
b6:=a6;
end;
end;
writeln('最多可以装',max:1:1,'吨,装以下货');
if b1 then write('5吨 ');
if b2 then write('2吨 ');
if b3 then write('35吨 ');
if b4 then write('1.7吨 ');
if b5 then write('1吨 ');
if b6 then write('5.1吨 ');
end.
运行结果如下:
最多可以装9.8吨,装以下货
2吨 1.7吨 1吨 5.1吨
第二种办法和第一种原理相同,但是对这六箱货是否装载进行二进制编码,结果一个0~63,这样可以用一重循环来实现,完整程序如下:
{$apptype console}
program temp;
var
a,b: integer;
max,sum:real;
begin
max:=0;
for a:=0 to 63 do
begin
sum:=0;
if a and 1<>0 then sum:=sum+5;
if a and 2<>0 then sum:=sum+2;
if a and 4<>0 then sum:=sum+3.5;
if a and 8<>0 then sum:=sum+1.7;
if a and 16<>0 then sum:=sum+1;
if a and 23<>0 then sum:=sum+5.1;
if (sum<=10)and(sum>max) then
begin
max:=sum;
b:=a;
end;
end;
writeln('最多可以装',max:1:1,'吨,装以下货');
if b and 1<>0 then write('5吨 ');
if b and 2<>0 then write('2吨 ');
if b and 4<>0 then write('35吨 ');
if b and 8<>0 then write('1.7吨 ');
if b and 16<>0 then write('1吨 ');
if b and 32<>0 then write('5.1吨 ');
end.
运行结果同上,即:
最多可以装9.8吨,装以下货
2吨 1.7吨 1吨
展开全部
高手
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
const x:array[1..6]of real=(5,2,3.5,1.7,1,5.1);
var a,b,c,d,e,f,h:integer;
k1,k2:real;
y:array[1..6] of integer;
begin
k1:=0;
k2:=0;
for a:=0 to 1 do begin
y[1]:=a;
for b:=0 to 1 do begin
y[2]:=b;
for c:=0 to 1 do begin
y[3]:=c;
for d:=0 to 1 do begin
y[4]:=d;
for e:=0 to 1 do begin
y[5]:=e;
for f:=0 to 1 do begin
y[6]:=f;
for h:=1 to 6 do begin
if y[h]=1 then begin
k1:=k1+x[h];
if (k1>k2)and(k1<10) then
k2:=k1;
if k1>10 then
k1:=k2;
end;
end;
end;
end;
end;
end;
end;
end;
writeln(k2:8:2);
end.
或
var max,sum:extended;
a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6:boolean;
begin
max:=0;
for a1:=false to true do begin
for a2:=false to true do begin
for a3:=false to true do begin
for a4:=false to true do begin
for a5:=false to true do begin
for a6:=false to true do begin
sum:=0;
if a1 then sum:=sum+5;
if a2 then sum:=sum+2;
if a3 then sum:=sum+3.5;
if a4 then sum:=sum+1.7;
if a5 then sum:=sum+1;
if a6 then sum:=sum+5.1;
if (sum<=10)and(sum>max) then begin
max:=sum;
b1:=a1;
b2:=a2;
b3:=a3;
b4:=a4;
b5:=a5;
b6:=a6;
end;
end;
end;
end;
end;
end;
end;
if b1 then writeln('5t',' ');
if b2 then writeln('2t',' ');
if b3 then writeln('35t',' ');
if b4 then writeln('1.7t',' ');
if b5 then writeln('1t',' ');
if b6 then writeln('5.1t',' ');
writeln('max:',max:1:1)
end.
var a,b,c,d,e,f,h:integer;
k1,k2:real;
y:array[1..6] of integer;
begin
k1:=0;
k2:=0;
for a:=0 to 1 do begin
y[1]:=a;
for b:=0 to 1 do begin
y[2]:=b;
for c:=0 to 1 do begin
y[3]:=c;
for d:=0 to 1 do begin
y[4]:=d;
for e:=0 to 1 do begin
y[5]:=e;
for f:=0 to 1 do begin
y[6]:=f;
for h:=1 to 6 do begin
if y[h]=1 then begin
k1:=k1+x[h];
if (k1>k2)and(k1<10) then
k2:=k1;
if k1>10 then
k1:=k2;
end;
end;
end;
end;
end;
end;
end;
end;
writeln(k2:8:2);
end.
或
var max,sum:extended;
a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6:boolean;
begin
max:=0;
for a1:=false to true do begin
for a2:=false to true do begin
for a3:=false to true do begin
for a4:=false to true do begin
for a5:=false to true do begin
for a6:=false to true do begin
sum:=0;
if a1 then sum:=sum+5;
if a2 then sum:=sum+2;
if a3 then sum:=sum+3.5;
if a4 then sum:=sum+1.7;
if a5 then sum:=sum+1;
if a6 then sum:=sum+5.1;
if (sum<=10)and(sum>max) then begin
max:=sum;
b1:=a1;
b2:=a2;
b3:=a3;
b4:=a4;
b5:=a5;
b6:=a6;
end;
end;
end;
end;
end;
end;
end;
if b1 then writeln('5t',' ');
if b2 then writeln('2t',' ');
if b3 then writeln('35t',' ');
if b4 then writeln('1.7t',' ');
if b5 then writeln('1t',' ');
if b6 then writeln('5.1t',' ');
writeln('max:',max:1:1)
end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询