有时会给出错误的答案

问题描述

每当我运行程序时,输入 2 for x 2 for y 它会产生 4,这很好,但是每当我输入 2 的 3 次方时,它输出 9它应该输出 8,当我做 3 的 2 次方时它输出 8,它应该给出 9。它适用于 2^2、2^4 但不适用于 2^3。同样 8^8 给出 256。程序写错了吗?

/ Start of the main program 
Input / Enter the exponent Store y
Subt One
Store Count

Input / Enter the Base
Store x
Store y
Jns Exp

/ Ending the main program
Load Ans 
Output 
End,Halt

Exp,Hex 0
Loop2,Load Count
    Skipcond 800
    JumpI Exp
    JnS Multiplier
    Load Ans
    Store x
    Load Count
    Subt One
    Store Count
    Jump Loop2

/ Start of the subroutine Multiplier
Multiplier,Hex 0
    Load Zero
    Store Ans
Loop,Load x
    Skipcond 800
    JumpI Multiplier
    Load Ans
    Add y
    Store Ans
    Load x
    Subt One
    Store x
    Jump Loop

/ Declaration
x,Dec 2
y,Dec 3
Zero,Dec 0
One,Dec 1 
Ans,Dec 0 
Count,Dec 0

解决方法

你的论点倒过来了。您对 2**33**2 的结果只是倒退。 2**22**4 不受参数顺序的影响。

用 16 位数字表示 8**8 是不可能的,因为那是 0x1000000,所以你总是会得到错误的结果。

您的代码首先取指数,然后取基数。


我使用 ** 约定表示指数而不是 ^,因为后者在大多数编程语言中用于表示异或。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...