標籤:
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 }
Reverse反轉演算法--C++實現