高手看看这个小程序怎么编啊~~

来源:百度知道 编辑:UC知道 时间:2024/05/22 04:31:09
1,1,2,3,5,8,13,21,34,55
传说中的裴波那契数列~~
怎么编程可以求出他的前10项和啊~~

你想用什么语言来编呢?
#include"stdio.h"
main(){
int v,s=0,a=1,b=1,i;
for(i=1;i<11;i++)
{s+=a;
v=a;a=b;b=v;
b+=a;
}
printf("前十项和=%d",s);
}

汇编代码:
mov ax,b800
mov es,ax
mov dx,0
mov ax,1
mov bx,1
mov cx,b
next:clc
add dx,ax
mov si,ax
mov ax,bx
mov bx,si
clc
add bx,ax
loop next
mov [500],dx
int 20h
;答案就在ds段,offset500单元处,要转成十进制看

如果你是用C,下面算法看起来比较容易理解
#include "stdio.h"
void main()
{
int i;
int s=0;
int f[10]={1,1};
for(i=2;i<10;i++)
f[i]=f[i-2]+f[i-1] ;
for(i=0;i<10;i++)
{
printf("%d,",f[i]);
s=s+f[i];
}
printf("\n总10项总和为%d",s);
}