C语言温习_求费氏级数
2009-09-15 21:48 youthjiang 阅读(569) 评论(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
7
#include <stdio.h>
8
#include <stdlib.h>
9
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
/*==============================================================================*/2
/*Program name: fibn.c */3
/*Program goal: design a program to generate the fibn */4
/*Author: Gene Jiang */5
/*==============================================================================*/6

7
#include <stdio.h>8
#include <stdlib.h>9

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



scanf(
浙公网安备 33010602011771号