编写一个计算(n)(n + 1)/ 2的程序。它应该从用户那里读取值“ n”。提示:您可以通过将一个和n之间的所有数字相加来计算此公式。
用HLA编写上述代码时遇到了挑战。我设法得到了以下
program printing_n_Numbers;
#include("stdlib.hhf");
static
n:int32;
i:int32;
begin printing_n_Numbers;
stdout.put("Enter n: ");
stdin.get(n);
mov(0,ecx)
stdout.put("printing ",n," Numbers ",nl);
for(mov(0,eax);eax<=n;add(1,eax)) do
for(mov(0,ebx);ebx<eax;add(1,ebx)) do
ecx = add(eax,ebx);
stdout.put("N was = ");
stdout.puti32(exc);
stdout.newln();
endfor;
endfor;
end printing_n_Numbers;
当我输入数字6时,输出为
Enter n: 6
printing 6 Numbers
N was = 1
N was = 2
N was = 3
N was = 4
N was = 5
N was = 2
N was = 4
N was = 6
N was = 3
N was = 6
N was = 4
N was = 8
N was = 5
N was = 6
我该如何编码以输出输入的数字总和?
答案 0 :(得分:0)
在进行了多次更改之后,程序开始运行。这就是我修改的方式
mov(0,ecx);
stdout.put("You Have Entered: ",n,nl);
for(mov(0,eax);eax<=n;add(1,eax)) do
add(eax,ecx);
endfor;
为了打印总和,这是代码
stdout.puti32(ecx);
我使用stdout.puti32
将十六进制转换为原始十进制数字系统