请编写复杂度为 O ( n 2 ) {\rm{O}}(n^2) O(n2)、 O ( n log n ) {\rm{O}}(n\log n) O(nlogn)、 O ( n ) {\rm{O}}(n) O(n)的任意程序,在不同问题规模下,记录运行时间,注明单位秒s或毫秒ms(写清运行代码的机器CPU配置,)。
CPU: Intel i7-7700HQ 2.80GHz
问题规模 n n n | O ( n 2 ) {\rm{O}}(n^2) O(n2) | O ( n log n ) {\rm{O}}(n\log n) O(nlogn) | O ( n ) {\rm{O}}(n) O(n) |
---|---|---|---|
1000 | 0.004s | - | |
10000 | 0.307s | - | |
20000 | 1.211s | 0.0009942s | |
40000 | 4.878s | 0.0010012s | |
100000 | 30.351s | 0.0009941s | |
1000000 | - | 0.003s | |
10000000 | - | 0.025s | |
100000000 | - | 0.224s | |
1000000000 | - | 2.366s |
O ( n 2 ) {\rm{O}}(n^2) O(n2)测试代码:
#include
#include
int main()
{
int n;
scanf("%d", &n);
clock_t st = clock();
long long sum = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
++sum;
clock_t ft = clock();
double dt = (ft - st) * 1.0 / CLOCKS_PER_SEC;
printf("sum = %lld\n", sum);
printf("duration: %f s\n", dt);
return 0;
}
O ( n log n ) {\rm{O}}(n\log n) O(nlogn)测试代码:
O ( n ) {\rm{O}}(n) O(n)测试代码:
#include
#include
int main()
{
int n;
scanf("%d", &n);
clock_t st = clock();
long long sum = 0;
for (int i = 0; i < n; ++i)
++sum;
clock_t ft = clock();
double dt = (ft - st) * 1.0 / CLOCKS_PER_SEC;
printf("sum = %lld\n", sum);
printf("duration: %f s\n", dt);
return 0;
}
请用递归和递推实现斐波那契数列第 n n n项的计算,结果对1000000007取模。
Fibonacci numbers:
F 0 = 0 , F 1 = 1 F_0 = 0, F_1 = 1 F0=0,F1=1
F n = F n − 1 + F n − 2 , ( n ≥ 2 ) F_n = F_{n-1} + F_{n - 2},(n \geq 2) Fn=Fn−1+Fn−2,(n≥2)
当 n n n为多大时,递归计算已经明显变慢了?
递归:当n为40的时候,有轻微卡顿,n每上升1,都有一个时间更长的卡顿。
递推:上亿级别就需要卡顿一下。
代码:
#include
#include
#include
using namespace std;
int F_1(int n)
{
if (n < 1)
return 0;
else if (n == 1 || n == 2)
return 1;
return F_1(n - 1) + F_1(n - 2);
}
int F_2(int n)
{
int a, b;
a = 1, b = 1;
for (int i = 3; i <= n; i++)
{
int t = b;
b = (a + b) % 1000000007;
a = t;
}
return b;
}
int main()
{
int N;
cin >> N;
clock_t st = clock();
cout << F_2(N);
clock_t ft = clock();
double dt = (ft - st) * 1.0 / CLOCKS_PER_SEC;
printf("duration: %lf s\n", dt);
return 0;
}
请编写 O ( n 3 ) {\rm{O}}(n^3) O(n3)、 O ( n 2 ) {\rm{O}}(n^2) O(n2)、 O ( n log n ) {\rm{O}}(n\log n) O(nlogn)的代码,自己生成一些测试数据进行本地测试,并分别在CG平台上提交。
代码:
#include
using namespace std;
int main()
{
int N;
scanf("%d", &N);
int a[N];
for (int i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
long long maxt = -1000000;
for (int i = 0; i < N; i++)
{
for (int j = i; j < N; j++)
{
long long sum = 0;
for (int k = j; k < N; k++)
{
sum += a[k];
maxt = max(maxt, sum);
}
}
}
printf("%d", maxt);
return 0;
}
#include
using namespace std;
int main()
{
int N;
scanf("%d", &N);
int a[N];
for (int i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
long long maxt = -1000000;
for (int i = 0; i < N; i++)
{
long long sum = 0;
for (int j = i ; j < N; j++)
{
sum += a[j];
maxt = max(maxt, sum);
}
}
printf("%d", maxt);
return 0;
}
#include
using namespace std;
const int MAX_N = 1e5 + 5;
int a[MAX_N];
int solve(int L, int R)
{
int mid = (L + R) / 2;
if (L == R)
{
return a[L];
}
int maxL = solve(L, mid);
int maxR = solve(mid + 1, R);
int mL, mR, sL, sR;
sL = sR = 0;
mL = mR = -100000;
for (int i = mid; i >= L; i--)
{
sL += a[i];
mL = max(mL, sL);
}
for (int i = mid + 1; i <= R; i++)
{
sR += a[i];
mR = max(sR, mR);
}
int maxmid = mR + mL;
return max(maxmid, max(maxL, maxR));
}
int main()
{
int N;
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%d", &a[i]);
}
printf("%d", solve(0, N - 1));
return 0;
}