java1到多少的阶乘小于9999_计算N的阶乘,最多能算到9999! | 学步园

c语言核心部分(来源网上):

#define N 8 //计算N的阶乘,最多能算到9999

long a[8916]={1,0},n,i,c,len;

int main()

{

n=N;

for ( len=1;n>1; n--)

{

for (c=0,i=0; i

{

long p= a[i]*n+c;

a[i]= p % 10000;

c=p / 10000;

}

a[i]= c;

if (c>0)

len++;

}

printf("%d",a[len-1]);

for( len--;len>=0;len--)

printf("%04d",a[len]);

system("Pause");

return 0;

} 我用汇编改写了一下: ;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

;-=- Factorial By G-Spider @2010

;-=- ml /c /coff Factorial.asm

;-=- link /subsystem:console Factorial.obj

;-=- invoke MessageBox,0,0,0,0

;-=- _Factorial函数最多能算到9999!

;-=- 按字符Q或q 退出

;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

.386

.model flat,stdcall

include user32.inc

include kernel32.inc

include msvcrt.inc

includelib user32.lib

includelib kernel32.lib

includelib msvcrt.lib

.data

fmt0 db '%d! = %d',0

fmt1 db '%04d',0

fmt2 db 0dh,0ah,0

inform db 'Please input num (0~999):',0dh,0ah,0

outform db 'Please Re-input num 0~999 Or Q-exit: ',0dh,0ah,0

szPause db 'Pause',0

dwStrlen dd 8916

.data?

lpOutputBuf dd ?

len dd ?

.code

;****************************

_Factorial proc lpA:dword,N:dword

local @c,@len

mov @len,1

mov esi,lpA

cmp N,0

je EXIT

Loop1:

mov @c,0

xor ebx,ebx

@@:

;p= a[i]*n+c;

mov eax,[esi+ebx*4]

imul eax,N

add eax,@c

cdq

mov ecx,10000 ;基为10000

idiv ecx

mov [esi+ebx*4],edx

mov @c,eax

inc ebx

cmp ebx,@len

jb @B

mov [esi+ebx*4],eax

cmp eax,0

jbe A00

inc @len

A00:

dec N

cmp N,1

ja Loop1

EXIT:

mov eax,@len

ret

_Factorial endp

;******************************

_Input proc

local @num

mov @num,0

@@:

invoke crt_getchar

mov ebx,eax

.if ebx=='q' || ebx =='Q'

invoke ExitProcess,0

.elseif ebx >= '0' && ebx <= '9'

mov ecx,@num

imul ecx,10

sub ebx,'0'

add ecx,ebx

mov @num,ecx

.elseif ebx==0ah

mov eax,@num

.if eax < 0 || eax >999 ;实际可以用>9999

invoke crt_printf,offset outform

mov @num,0

jmp @B

.endif

ret

.endif

jmp @B

_Input endp

;******************************

_OutPut proc lpBuf:dword,@num:dword

invoke _Factorial,lpBuf,@num

dec eax

push eax

push esi

mov esi,lpBuf

invoke crt_printf,offset fmt0,@num,dword ptr[esi+eax*4]

pop esi

pop eax

test eax,eax

jz EXIT

dec eax

@@:

push eax

push esi

invoke crt_printf,offset fmt1,dword ptr[esi+eax*4]

pop esi

pop eax

dec eax

jns @B

EXIT:

invoke crt_printf,offset fmt2

xor eax,eax

ret

_OutPut endp

;******************************

start:

invoke crt_printf,offset inform

mov eax,dwStrlen

shl eax,2

mov dwStrlen,eax

invoke crt_malloc,eax

mov lpOutputBuf,eax

@@:

invoke RtlZeroMemory,lpOutputBuf,dwStrlen

mov esi,lpOutputBuf

mov dword ptr [esi],1

invoke _Input

invoke _OutPut,lpOutputBuf,eax

jmp @B

end start

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值