wenbao与面试题
百度面试题
O(n)的时间求出n个数中连续区间中连续的数
辉神就是屌。。。。竟然可以用方差。。。。
方差(求一组数据的离散程度)
1 #include <iostream> 2 #include <time.h> 3 #include <stdio.h> 4 using namespace std; 5 const int maxn = 100; 6 int a[maxn], b[maxn], sum, x, n; 7 bool flag = false; 8 int main(){ 9 scanf("%d", &n); 10 for(int i = 1; i <= n; ++i){ 11 scanf("%d", &a[i]); 12 b[i] = a[i]*a[i]; 13 } 14 sum = b[1] + b[2] + b[3] + b[4]; 15 x = a[1] + a[2] + a[3] + a[4]; 16 for(int i = 5; i <= n; ++i){ 17 sum = sum - b[i-5] + b[i]; 18 x = x - a[i-5] + a[i]; 19 if(x % 5) continue; 20 if(sum - x/5*x == 10){ 21 printf("%d----->%d\n", i-4, i); 22 flag = true; 23 } 24 } 25 if(!flag) printf("No\n"); 26 printf("%.3lf\n", (double)clock()/CLOCKS_PER_SEC); 27 return 0; 28 }
只有不断学习才能进步!