gpt4 book ai didi

algorithm - Pascal 编程帮助

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:27:29 25 4
gpt4 key购买 nike

我之前发布了这个但它被关闭了,因为我没有展示我对它的编码尝试,所以这里是问题:

SECTIONS
$160 = section 1
$220 = section 2
$280 = section 3
$350 = section 4
$425 = section 5

开发伪代码,接受未指定数量的伪装者的姓名作为输入,每个伪装者都支付了他们服装的全部费用以及每个人支付的金额。

化装舞者可能已经为乐队五个部分中的任何一个部分支付了服装费用。该算法应根据他/她为服装支付的金额来确定伪装者扮演的部分。该算法还应确定在每个部分中为服装付费的化妆舞会者的数量。

应打印人员姓名和他们已支付的部分。还应打印每个部分的部分列表和注册参加游戏的总人数,以及每个部分支付的总金额。

这是我的尝试:*注意这是用 Pascal 编程的,我需要帮助来修复它并完成它。请帮助并再次感谢。

program Masqueraders;

uses
WinCrt; { Allows Writeln, Readln, cursor movement, etc. }

const
MAX = 5; {this determine the amount of masquarader entered}
Type
listname = Array[1..MAX] of string;
listsect = Array[1..MAX] of string;
var
names : listname;
sections : listsect;
i, amount, TotalMas, TotalAmt, c1, c2, c3, c4, c5, amt1, amt2, amt3, amt4, amt5 : integer;

begin

amount := 1;
while amount <> 0 do
begin

i := i + 1;
readln(names[i]);
readln(amount);

if(amount = 160) then
begin

c1 := c1 + 1; {Count the number of persons for section 1}
amt1 := amt1 + amount; {accumulate the amount for section 1}
sections[i] := 'Section 1';
end;

if(amount = 220) then
begin

c2 := c2 + 1; {Count the number of persons for section 1}
amt2 := amt2 + amount; {accumulate the amount for section 1}
sections[i] := 'Section 2';
end; {end the IF for section 2}

if(amount = 280) then
begin

c3 := c3 + 1; {Count the number of persons for section 1}
amt3 := amt3 + amount; {accumulate the amount for section 1}
sections[i] := 'Section 3';
end; {end the IF for section 3}

if(amount = 350) then
begin

c4 := c4 + 1;
amt4 := amt4 + amount;
sections[i] := 'Section4';
end; {end If for section 4}

if (amount = 425) then
begin

c5 := c5 + 1;
amt5 := amt5 + amount;
sections[i] := 'Section5';


end;{end the while loop}

TotalMas := c1 + c2 + c3;
TotalAmt := amt1 + amt2 + amt3;


writeln('Name Section'); {Heading for the output}
for i := 1 to MAX do
begin

write(names[i]);
writeln(' ',sections[i]);

end;


writeln('Section 1: ');
write('Masquader: ', c1);
write('Amount: ', amt1);



writeln('Total Number of Masquarader: ', TotalMas);
writeln('Total Amount Paid by masquarader: ', TotalAmt);

结束; 结束。

简而言之,它应该接受一个不确定数量的人,并根据他们输入的金额将他们分配到各自的部分,然后计算每个部分的人数。这是我当前的输出:

Name John Money=160 Section 1

Name Keith Money=220 Section John

这就是我想要的:

Name John Money=160 Section1

Name Keith Money=220 Section2

最佳答案

我不太熟悉 Pascal,所以我不能告诉你如何解决这个问题,但我注意到一个问题是你的代码似乎违反了“DRY”规则:不要重复你自己。每个 if amt = xxx block 中的代码看起来几乎完全相同,那么有没有一种方法可以编写一次该代码,然后每次都使用不同的参数调用您的代码?这样您就不会将相同的代码重写五次。

关于algorithm - Pascal 编程帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2354060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com