C Fundamentals of Data Structures and Their Applications Slides: 11 Download presentation 資料結構設計與C++程式應用 Fundamentals of Data Structures and Their Applications Using C++ 第 5章 遞迴 資料結構設計與C++程式應用 版權所有 禁止重製 遞迴 • 常見的遞迴應用 –求最大公因數GCD // 求GCD之遞迴函數 int gcd(int dividend, int divisor) { int remainder; if (dividend < divisor){ // 找出兩數之大者當被除數 swap(dividend, divisor); // 小者當除數 } if (divisor !=0) { remainder = dividend % divisor; return gcd(divisor, remainder); } else return dividend; } 資料結構設計與C++程式應用 // 以參考值傳遞引數,將x, y兩數對調 void swap(int &x, int &y) { int temp = x; x = y; y = temp; } 版權所有 禁止重製