Reverse反轉演算法--C++實現

來源:互聯網
上載者:User

標籤:

 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++實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.