标签:
Reverse反转算法
1 #include <iostream> 2 3 using namespace std; 4 //交换的函数 5 void replaced(int &a,int &b){ 6 int t = a; 7 a = b; 8 b = t; 9 } 10 //反转 11 void reversed(int a[],int length){ 12 int left = 0; 13 int right = length - 1; 14 while (left < right) { 15 replaced(a[left], a[right]); 16 left++; 17 right--; 18 } 19 } 20 void output(int a[],int length) 21 { 22 for (int i = 0; i<length; i++) { 23 cout << a[i] << " "; 24 } 25 } 26 int main() 27 { 28 int a[] = {1,2,3,4,5,6,7,8,9}; 29 output(a, 9); 30 cout << endl; 31 reversed(a, 9); 32 output(a, 9); 33 }
斐波那契数列
1 #include <iostream> 2 3 using namespace std; 4 5 //斐波那契数列 6 int qiebona(int a) 7 { 8 //也可以用if语句 9 switch (a) { 10 case 1: 11 case 2: 12 return a; 13 break; 14 15 default: 16 return qiebona(a-1)+qiebona(a-2); 17 break; 18 } 19 } 20 int main() 21 { 22 //验证斐波那契函数 23 cout << qiebona(1) << endl; 24 //然后打印前n个数的斐波那契数列 25 for (int i = 1; i <= 10; i++) { 26 cout << qiebona(i) << " "; 27 } 28 return 0; 29 }
标签:
原文地址:http://www.cnblogs.com/goodboy-heyang/p/4696574.html