You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
6 years ago | |
|---|---|---|
| .. | ||
| .vscode | 6 years ago | |
| CMakeLists.txt | 6 years ago | |
| Partition.txt | 6 years ago | |
| PartitionPerf.h | 6 years ago | |
| PartitionTest.cpp | 6 years ago | |
| YourFib.h | 6 years ago | |
| YourPartition.h | 6 years ago | |
| hw1Test.cpp | 6 years ago | |
| hw1_2.xlsx | 6 years ago | |
| readme.txt | 6 years ago | |
readme.txt
Value:
void Fib::Value(const int &n) //计算并设置斐波那契数列第n项的值value
{
int a = 0, b = 1;
for (int i = 0; i < n; i++)
{
int t = a + b;
a = b, b = t;
}
value = a;
}
时间复杂度O(n),空间复杂度O(1)。
有基于矩阵快速幂的O(logn)做法,
然而在结果在int范围内的情况下常数较大,
这里不列出了。
Partition:
int Partition(int x[], int n, int &movenum, int &compnum, int &bytesnum)
{
bytesnum = 2 * sizeof(int);
int i = 0;
for (int j = 1; j < n; j++)
{
compnum++;
if (x[j] < x[0])
swap(x, ++i, j), movenum += 3;
}
swap(x, 0, i), movenum += 3;
return i;
}
时间复杂度O(n),空间复杂度O(1)。
将整个数组就地分成大于x[0]和小于x[0]的两部分。