原创 插入排序及歸併排序

插入排序1: void InsertSort_1(int Array[], int n) { for (int i = 1; i < n; ++i) { int key = Array[i]; int j = i - 1;

原创 兩相異正數的等差中項和等比中項

#include <iostream> #include <cmath> #include <iomanip> #include <vector> using namespace std; //求數列an的第n項double Ar

原创 一個運行時間爲nlgn的算法,能判斷在集合S中是否有兩數之和爲x

先用歸併排序對集合S中的整數進行排序,接着再用下面的子過程進行判斷: bool Func(int Array[], int first, int last, int x) { while (first < last) { if

原创 求常數e

//計算階乘double Factorial(int n) { if (n == 2) { return 2.0; } return n * Factorial(n - 1); } //計算常數e double Calcu

原创 求序列中連續相鄰元素之和最大值的四種算法

第一種是最直觀的算法,把每一種連續相鄰的和全都算出來進行比較: #include <vector> using namespace std; int maxSubSum1(const vector<int>& a) { int m

原创 用程序求倒數

double reciprocal(double c) { static int n = 10; if (--n) { double temp = reciprocal(c); return temp * (2 - te