We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
func LoopAdd(cnt, v0, step int) int { result := v0 for i := 0; i < cnt; i++ { result += step } return result }
比如 1+2+...+100 等差数列可以这样计算 LoopAdd(100, 1, 1),而 10+8+...+0 等差数列则可以这样计算 LoopAdd(5, 10, -2)。
按这个算法 LoopAdd(100, 1, 1) = 101 期望:5050 LoopAdd(5, 10, -2) = 0 期望:30
The text was updated successfully, but these errors were encountered:
修改后:
func LoopAdd(cnt, v0, step int) int { result := v0 next := v0 for i := 1; i < cnt; i++ { next += step result += next } return result }
汇编:
TEXT ·LoopAdd(SB),$0-32 MOVQ cnt+8*0(FP), AX // cnt MOVQ v0+8*1(FP), BX // v0 /result MOVQ step+8*2(FP), CX // step MOVQ BX, R8 // next MOVQ $1, DX // i LOOP_IF: CMPQ DX, AX // compare i, cnt JL LOOP_BODY // if i < cnt; goto LOOP_BODY JMP LOOP_END LOOP_BODY: ADDQ CX, R8 // next += step ADDQ R8, BX // result += next ADDQ $1, DX // i++ JMP LOOP_IF LOOP_END: MOVQ BX, ret+8*3(FP) // return result RET
Sorry, something went wrong.
发现 BUG 直接上 PR
chai2010
No branches or pull requests
问题描述
按这个算法
LoopAdd(100, 1, 1) = 101 期望:5050
LoopAdd(5, 10, -2) = 0 期望:30
The text was updated successfully, but these errors were encountered: