google笔试题收集



1. 异或
1^1=0
0^0=0
1^0=1
0^1=1

2. 斐波那契数列(Fibonacci)

3.存储空间上不具有优势

4. 由“主定理”
T(n)=25T(n/5) + n2

a=25  b=5   f(n)=n2
logba=2

将nlogba与f(n)比较: 相等
则时间复杂度为O(n2logn)



1.

 

2. 

3

#include <iostream>
#include 
<limits>
using namespace std;

int foo(int arr[], int n)
{
    
int max = numeric_limits<int>::min();
    
//cout << max << "\n";
    for(int i = 0; i < n; i++)
    {
        
int product = 1;
        
for(int j =0; j < n; j++)
        {
            
if(j != i )
                product 
*= arr[j];            
        }
        max 
= product > max ? product : max;
    }

    
return max;
}

int main()
{
    
int arr[4= { -1-523};
    cout 
<< foo(arr, 4<< "\n";

    
return 0;
}

时间复杂度分析:
总共的操作步骤 n*{[n+(n-1)]+1}=2n2
时间复杂度为O(n2)

空间复杂度为O(1)  4*sizeof(int)


改进算法:
 

posted @ 2007-09-16 16:04  中土  阅读(3816)  评论(2编辑  收藏  举报
©2005-2008 Suprasoft Inc., All right reserved.