C语言温习_求费氏级数
2009-09-15 21:48 youthjiang 阅读(559) 评论(0) 编辑 收藏 举报
主要通过该函数,温习了if, Swith, for等语句,同时温习了递归函数的调用。此外,该函数的所有case情形,得到了考虑。
为什么用swith来代替if,主要是从性能上面进行考虑的,如果在嵌入式系统中,一点性能的提高,都是值得我们去做的。
自问自答:
1) 为什么用swith会提高性能?
代码如下:
为什么用swith来代替if,主要是从性能上面进行考虑的,如果在嵌入式系统中,一点性能的提高,都是值得我们去做的。
自问自答:
1) 为什么用swith会提高性能?
代码如下:
1
/*==============================================================================*/
2
/*Program name: fibn.c */
3
/*Program goal: design a program to generate the fibn */
4
/*Author: Gene Jiang */
5
/*==============================================================================*/
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
#include <stdio.h>
8
#include <stdlib.h>
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
int main(int argc, char *argv[])
11
{
12
int nNumber;
13
int nFib0 =0;
14
int nFib1 =1;
15
int nFib =0;
16
int i;
17
18
printf("The Fibonacci Numbers\n");
19
printf("Please enter a number : ");
20
scanf("%d", &nNumber); /*Input the Vaule*/
21
22
if (nNumber < 0)
23
{
24
printf("Please input one number that is above the 0\n");
25
26
system("PAUSE");
27
return 0;
28
}
29
30
switch(nNumber)
31
{
32
case 0:
33
printf("Fibonacci Numbers of %d = 0\n", nNumber);
34
break;
35
case 1:
36
printf("Fibonacci Numbers of %d = 1\n", nNumber);
37
break;
38
}
39
40
if ( nNumber >=2)
41
{
42
for (i = 2; i<= nNumber; i++)
43
{
44
nFib = nFib0 + nFib1;
45
nFib0 = nFib1;
46
nFib1 = nFib;
47
}
48
49
printf("Fibonacci Number of %d = %d\n", nNumber, nFib);
50
}
51
52
53
system("PAUSE");
54
return 0;
55
}
56![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)