谷歌2013年面試題(參考)

http://blog.csdn.net/hackbuteer1/article/details/8017703

 

Google2012.9.24校園招聘會筆試題

分類: 劍指Offer 8423人閱讀 評論(17) 收藏 舉報





代碼:

  1. //轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/8017703  
  2. bool IsPrime(int n)  
  3. {  
  4.     int i;  
  5.     if(n < 2)  
  6.         return false;  
  7.     else if(2 == n)  
  8.         return true;  
  9.     if((n&1) == 0)    //n%2 == 0  
  10.         return false;  
  11.     for(i = 3 ; i*i <= n ; i += 2)     //只考慮奇數  
  12.     {  
  13.         if(n % i == 0)  
  14.             return false;  
  15.     }  
  16.     return true;  
  17. }  
  18.   
  19. /* 
  20. 考慮到所有大於4的質數,被6除的餘數只能是1或者5 
  21. 比如接下來的5,7,11,13,17,19都滿足 
  22.  
  23. 所以,我們可以特殊化先判斷2和3 
  24. 但後面的問題就出現了,因爲並非簡單的遞增,從5開始是+2,+4,+2,+4,....這樣遞增的 
  25. 這樣的話,循環應該怎麼寫呢? 
  26.  
  27. 首先,我們定義一個步長變量step,循環大概是這樣 for (i = 5; i <= s; i += step) 
  28. 那麼,就是每次循環,讓step從2變4,或者從4變2 
  29. 於是,可以這麼寫: 
  30. */  
  31. bool IsPrime2(int n)  
  32. {  
  33.     int i, step = 4;  
  34.     if(n < 2)  
  35.         return false;  
  36.     else if(2 == n || 3 == n)  
  37.         return true;  
  38.     if((n&1) == 0)    //n%2 == 0  
  39.         return false;  
  40.     if(n%3 == 0)      //n%3 == 0  
  41.         return false;  
  42.     for(i = 5 ; i*i <= n ; i += step)  
  43.     {  
  44.         if(n % i == 0)  
  45.             return false;  
  46.         step ^= 6;  
  47.     }  
  48.     return true;  
  49. }  
  50.   
  51. void print_prime(int n)  
  52. {  
  53.     int i , num = 0;  
  54.   
  55.     for(i = 0 ; ; ++i)  
  56.     {  
  57.         if(IsPrime2(i))  
  58.         {  
  59.             printf("%d  " , i);  
  60.             ++num;  
  61.             if(num == n)  
  62.                 break;  
  63.         }  
  64.     }  
  65.     printf("\n");  
  66. }  


代碼:

  1. //轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/8017703  
  2. void myswap(int a , int b , int* array)  
  3. {  
  4.     int temp = array[a];  
  5.     array[a] = array[b];  
  6.     array[b] = temp;  
  7. }  
  8.   
  9. //利用0和其它數交換位置進行排序  
  10. void swap_sort(int* array , int len)  
  11. {  
  12.     int i , j;  
  13.     for(i = 0 ; i < len ; ++i)          //因爲只能交換0和其他數,所以先把0找出來  
  14.     {  
  15.         if(0 == array[i])  
  16.         {  
  17.             if(i)   //如果元素0不再數組的第一個位置  
  18.                 myswap(0 , i , array);  
  19.             break;  
  20.         }  
  21.     }  
  22.   
  23.     for(i = 1 ; i < len ; ++i)     //因爲是0至N-1的數,所以N就放在第N的位置處  
  24.     {  
  25.         if(i != array[i])    //這個很重要,如果i剛好在i處,就不用交換了,否則會出錯  
  26.         {  
  27.             for(j = i + 1 ; j < len ; ++j)  
  28.             {  
  29.                 if(i == array[j])  
  30.                 {  
  31.                     myswap(0 , j , array);   //把0換到j處,此時j處是0  
  32.                     myswap(j , i , array);   //把j處的0換到i處,此時i處是0  
  33.                     myswap(0 , i , array);   //把i處的0換到0處  
  34.                 }  
  35.             }//for  
  36.         }  
  37.     }//for  
  38. }  


代碼:
  1. //轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/8017703  
  2. int mymin(int a , int b , int c)  
  3. {  
  4.     int temp = (a < b ? a : b);  
  5.     return temp < c ? temp : c;  
  6. }  
  7.   
  8. int min_edit_dic(char* source , char* target)  
  9. {  
  10.     int i , j , edit , ans;  
  11.     int lena , lenb;  
  12.     lena = strlen(source);  
  13.     lenb = strlen(target);  
  14.     int** distance = new int*[lena + 1];  
  15.     for(i = 0 ; i < lena + 1 ; ++i)  
  16.         distance[i] = new int[lenb + 1];  
  17.     distance[0][0] = 0;  
  18.     for(i = 1 ; i < lena + 1 ; ++i)  
  19.         distance[i][0] = i;  
  20.     for(j = 1 ; j < lenb + 1 ; ++j)  
  21.         distance[0][j] = j;  
  22.     for(i = 1 ; i < lena + 1 ; ++i)  
  23.     {  
  24.         for(j = 1 ; j < lenb + 1 ; ++j)  
  25.         {  
  26.             if(source[i - 1] == target[j - 1])  
  27.                 edit = 0;  
  28.             else  
  29.                 edit = 1;  
  30.             distance[i][j] = mymin(distance[i - 1][j] + 1 , distance[i][j - 1]  + 1 , distance[i - 1][j - 1] + edit);  
  31.             //distance[i - 1][j] + 1             插入字符  
  32.             //distance[i][j - 1]  + 1            刪除字符  
  33.             //distance[i - 1][j - 1] + edit      是否需要替換  
  34.         }  
  35.     }  
  36.     ans = distance[lena][lenb];  
  37.   
  38.     for(i = 0 ; i < lena + 1 ; ++i)  
  39.         delete[] distance[i];  
  40.     delete[] distance;  
  41.   
  42.     return ans;  
  43. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章