C语言 写程序,输出斐波那契数列的前20项,每行输出4个数
答案:3 悬赏:60 手机版
解决时间 2021-12-01 01:14
- 提问者网友:書生途
- 2021-11-30 21:43
C语言 写程序,输出斐波那契数列的前20项,每行输出4个数
最佳答案
- 五星知识达人网友:拾荒鲤
- 2021-11-30 22:59
#include
int main(void) {
int n; // 输入一个数字 N
int i; // 第 i 个将要输出的斐波那契数
int current; // 第 i 个斐波那契数的值
int next; // 下一个(i+1)斐波那契数的值
int twoaway; // 下下个(i+2)斐波那契数的值
printf("您需要斐波那契数列的前几项?请输入: ");
scanf("%d", &n);
if (n<=0)
printf("请输入正整数!
");
else {
printf("前%d项斐波那契数列为:
",n);
next = current = 1;
for (i=1; i<=n; i++) {
printf("%d ",current);
if (i%4==0) printf("
");// 控制每输出4个数换一行
twoaway = current+next;
current = next;
next = twoaway;
}
}
}执行结果
输入:20,即题主希望输出前20项斐波那契数列。这里注意,斐波那契数列“第0项”为“0”,这里以第1项开始。
int main(void) {
int n; // 输入一个数字 N
int i; // 第 i 个将要输出的斐波那契数
int current; // 第 i 个斐波那契数的值
int next; // 下一个(i+1)斐波那契数的值
int twoaway; // 下下个(i+2)斐波那契数的值
printf("您需要斐波那契数列的前几项?请输入: ");
scanf("%d", &n);
if (n<=0)
printf("请输入正整数!
");
else {
printf("前%d项斐波那契数列为:
",n);
next = current = 1;
for (i=1; i<=n; i++) {
printf("%d ",current);
if (i%4==0) printf("
");// 控制每输出4个数换一行
twoaway = current+next;
current = next;
next = twoaway;
}
}
}执行结果
输入:20,即题主希望输出前20项斐波那契数列。这里注意,斐波那契数列“第0项”为“0”,这里以第1项开始。
全部回答
- 1楼网友:污到你湿
- 2021-12-01 01:15
#include
#includeint fibonacci(int n);
void createFibArray(int *buf, int size);
int printArray(FILE *fp, const int *buf, int size, int colum, const char *columseperator, const char *rowseperator);int main()
{
const int SIZE = 20;
int *fibArray = (int *)malloc(sizeof(int) * SIZE);
if (fibArray == NULL)
{
return -1;
}
createFibArray(fibArray, SIZE);
printArray(stdout, fibArray, SIZE, 4, "\t", "\n");
return 0;
}int fibonacci(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
int a1 = 0, a2=1;
int i = 0;
for (i = 0; i {
a2 = a1 + a2;
a1 = a2 - a1;
}
return a2;
}void createFibArray(int *buf, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
buf[i] = fibonacci(i);
}
}int printArray(FILE *fp, const int *buf, int size, int colum, const char *columseperator, const char *rowseperator)
{
int i = 0;
int count = 0;
for (i = 0; i {
count += fprintf(fp,"%d",buf[i]);
if (i == size -1)
{
continue;
}
if ((i+1)%colum == 0)
{
count += fprintf(fp, rowseperator);
}
else
{
count += fprintf(fp, columseperator);
}
}
count +=fprintf(fp,"\n");
return count;
}
#include
void createFibArray(int *buf, int size);
int printArray(FILE *fp, const int *buf, int size, int colum, const char *columseperator, const char *rowseperator);int main()
{
const int SIZE = 20;
int *fibArray = (int *)malloc(sizeof(int) * SIZE);
if (fibArray == NULL)
{
return -1;
}
createFibArray(fibArray, SIZE);
printArray(stdout, fibArray, SIZE, 4, "\t", "\n");
return 0;
}int fibonacci(int n)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
int a1 = 0, a2=1;
int i = 0;
for (i = 0; i
a2 = a1 + a2;
a1 = a2 - a1;
}
return a2;
}void createFibArray(int *buf, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
buf[i] = fibonacci(i);
}
}int printArray(FILE *fp, const int *buf, int size, int colum, const char *columseperator, const char *rowseperator)
{
int i = 0;
int count = 0;
for (i = 0; i
count += fprintf(fp,"%d",buf[i]);
if (i == size -1)
{
continue;
}
if ((i+1)%colum == 0)
{
count += fprintf(fp, rowseperator);
}
else
{
count += fprintf(fp, columseperator);
}
}
count +=fprintf(fp,"\n");
return count;
}
- 2楼网友:英雄的欲望
- 2021-12-01 00:37
#include
int main()
{
int f1=1,f2=1;
int i;
for(i=1;i<=20;++i)
{
printf("%-12d%-12d",f1,f2);
if(i%2==0)
printf("\n");
f1=f1+f2;
f2=f1+f2;
}
printf("\n");
return 0;
}
int main()
{
int f1=1,f2=1;
int i;
for(i=1;i<=20;++i)
{
printf("%-12d%-12d",f1,f2);
if(i%2==0)
printf("\n");
f1=f1+f2;
f2=f1+f2;
}
printf("\n");
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |