C语言零基础入门习题(九)斐波那契数列

阅读: 评论:0

C语言零基础入门习题(九)斐波那契数列

C语言零基础入门习题(九)斐波那契数列

前言 

        C语言是大多数小白走上程序员道路的第一步,在了解基础语法后,你就可以来尝试解决以下的题目。放心,本系列的文章都对新手非常友好。


Tips:题目是英文的,但我相信你肯定能看懂

一、The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13... where the first two terms are 0 and 1, and each term thereafter is defined recursively as the sum of the two preceding terms; that is

Fib(n)=n for n< 2

Fib{n) = Fib(n-1) + Fib(n-2) for n >= 2

a. Write a recursive function that returns the nth number in a Fibonacci sequence when n is passed to the function as an argument.

For example, when n = 8, the function returns the 8th number in the sequence, which is 13.

#include <stdio.h>
int fib(int);int main()
{int n;scanf("%d",&n);printf("%d",fib(n));return 0;
}
int fib(int n)
{int s;if(n<3)   return n-1;return fib(n-1)+fib(n-2);
}

b. Write a function that uses repetition without array to calculate the nth term of a Fibonacci sequence.

#include <stdio.h>int main()
{int n,a=1,b=0,c=1,i;scanf("%d",&n);if(n<3)   a=n-1;else{for(i=3;i<=n;i++){a=b+c;b=c;c=a;}}printf("%d",a);return 0;
}

c. Write a function that uses repetition and array to calculate the nth term of a Fibonacci sequence.

#include <stdio.h>int main()
{int a[9999];int n,i;scanf("%d",&n);for(i=1;i<=n;i++){if(i<3)   a[i]=i-1;else    a[i]=a[i-1]+a[i-2];}printf("%d",a[n]);return 0;
}

二、Write a program that will read five values from the keyboard and store them in an array of type float with the name amounts.

Create two arrays of five elements of type long with the names dollars and cents. Store the whole number part of each value in the amounts array in the corresponding element of dollars and the fractional part of the amount as a two-digit integer in cents (e.g., 2.75 in amounts[1] would result in 2 being stored in dollars[1] and 75 being stored in cents[1]). Output the values from the two arrays of type long as monetary amounts.

#include <stdio.h>int main()
{float amounts[4];long dollars[4], cents[4];int i;for(i=0;i<5;i++){scanf("%f",&amounts[i]);dollars[i]=(int)amounts[i];cents[i]=(amounts[i]-dollars[i])*100;printf("dollar=%ld, cent=%ldn",dollars[i], cents[i]);}return 0;
}

三、Write a C program that includes two functions named calcAvg() and variance ().

The calcAvg() func-tion should calculate and return the average of the values stored in an array named testvals. The array should be declared in main () and include the values 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73. The variance () function should calculate and return the variance of the data. The variance is obtained by subtracting the average from each value in testvals, squaring the differences obtained, adding their squares, and dividing by the number of elements in testvals. The values returned from calcAvg() and variance () should be displayed using printf () function calls in main ()。

#include <stdio.h>
void calcAvg(int[], float*);
void variance (int[], float*);int main()
{float a,b;int testvals[14]={89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73};calcAvg(testvals, &a);variance (testvals, &b);printf("average = %gn",a);printf("variance = %g",b);return 0;
}
void calcAvg(int t[], float* ps)
{int i;float s=0;for(i=0;i<14;i++)s+=t[i];*ps=s/14;
}
void variance (int t[], float* pv)
{int i;float v=0;for(i=0;i<14;i++)v=v+(t[i]-80.5)*(t[i]-80.5);*pv=v/14;
}

四、Write a program that prompts the user to enter three sets of five double numbers each.

(You may assume the user responds correctly and doesn’t enter non-numeric data.) The program should accomplish all of the following:

  1. Store the information in a 3×5 array.
  2. Compute the average of each set of five values.
  3. Compute the average of all the values.
  4. Determine the largest value of the 15 values.
  5. Report the results.

Each major task should be handled by a separate function using the traditional C approach to handling arrays. Accomplish taskbby using a function that computes and returns the average of a one-dimensional array; use a loop to call this function three times. The other tasks should take the entire array as an argument, and the functions performing taskscand dshould return the answer to the calling program.

#include <stdio.h>
void ave(double [3][5], double*, double*);
void max(double [3][5], double*);int main()
{double a[3][5],b[3];double a1,m;int i,j;printf("pls enter three sets of five double numbers each:");for(i=0;i<3;i++)for(j=0;j<5;j++)scanf("%lf",&a[i][j]);ave(a,&a1,&b);printf(" the average of each set of five values:%g %g %gn",b[0],b[1],b[2]);printf(" the average of all the values:%gn",a1);max(a,&m);printf("the largest value of the 15 values:%gn",m);return 0;
}
void ave(double a[3][5], double*a1,double*b)
{int i,j;double s=0;for(i=0;i<3;i++)for(j=0;j<5;j++){s+=a[i][j];b[i]+=a[i][j];}for(i=0;i<3;i++)b[i]=b[i]/5;*a1=s/15;
}
void max(double a[3][5], double*m)
{int i,j;*m=a[0][0];for(i=0;i<3;i++){for(j=0;j<5;j++)if(a[i][j]>*m)   *m=a[i][j];}
}

总结

以上就是本文全部内容,你学会了吗?

本文发布于:2024-01-28 06:52:31,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063959555594.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数列   习题   入门   语言   基础
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23