2、从键盘输入n(>2),计算斐波那契数列的前n项并按每行10个数据输出。
http://www.2cto.com/kf/201306/222126.html
公式都有,自己循环n,带入公式计算一下就好了
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
int n = 0;
scanf_s("%d", &n);
int a = 0; int b = 1;
for (int i = 1; i < n; i++)
{
int x = b;
printf("%d%\t", x);
if (i % 10 == 0) printf("\n");
b = a + b;
a = b - a;
}
return 0;
}
45
1 1 2 3 5 8 13 21 34 55
89 144 233 377 610 987 1597 2584 4181 6765
10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169
63245986 102334155
165580141 267914296 433494437 701408733 Press any key to
continue . . .
#include
int f(int n)
{
if (n==1|| n==2)
{
return (1);
}
else
{
return (f(n-1)+f(n-2));
}
}
void main()
{
int n,i;
printf("input n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d\n",f(i));
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 0;
scanf_s("%d", &n);
int a = 0; int b = 1;
for (int i = 1; i < n; i++)
{
int x = b;
printf("%d%\t", x);
if (i % 10 == 0) printf("\n");
b = a + b;
a = b - a;
}
return 0;
}