前言
我記得c++考試也是考了90多分的人,現在重新複習,感覺有點手生了。
基礎
主要是函數與參數,動態內存空間分配的知識,STL會在後面的數據結構中進一步複習。
重點
關於遞歸,在學習時花了好長時間去理解,但自己用的不多,重新複習一下吧。
#include <iostream>
using namespace std;
int factorial(int n) //遞歸求階乘
{
if (n<=1)
return 1;
else
return n*factorial(n-1);
}
int sum(int a[],int n)//遞歸求和
{
if(n > 0)
return a[n-1]+sum(a,n-1);
return 0;
}
int greatestCommonDivisor(int x,int y)//歐幾里得算法求最大公約數
{
if(y == 0)
return x;
return greatestCommonDivisor(y,x%y);
}
int main()
{
int a[] = {1,2,3};
cout<<factorial(3)<<endl;
cout<<sum(a,3)<<endl;
cout<<greatestCommonDivisor(15,30)<<endl;
return 0;
}