歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

網易有道面經(2013校園招聘杭州站)

上機考試:

網易有道的篩選模式是先上機考試,然後根據上機考試選擇大概1/3參加面試。上機的平台和ACM有點類似,提交代碼然後有手動閱卷。

上機考試時隔比較久遠,不過還能想起兩個題目:

1. 給定一個點分IP地址表示,寫個程序把它轉換成相應的32位的無符號整數並輸出,如果輸入不是合法數據,就返回0.

這個題目如何利用好標准輸入輸出,其實可以很容易判斷出不合法的輸入用例,不過當時沒有想好,導致這個題目沒有AC。

後來回去寫的代碼如下:

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. bool checkpoint(char *str){ 
  4.  
  5.     int npoint = 0; 
  6.     while(*str){ 
  7.         (*str) == '.' ? npoint++ : npoint; 
  8.         if(*(str) != '.' && !((*str) <= '9' && (*str) >= '0')) return false
  9.         str++; 
  10.     } 
  11.     return npoint == 3; 
  12.  
  13. bool checkinrange(int addr[4]){ 
  14.     for(int i = 0; i < 4; i++){ 
  15.         if(addr[i] > 255){ 
  16.             return false
  17.         } 
  18.     } 
  19.     return true
  20. bool convertIP(char s[], int addr[4]){ 
  21.     char tmp[128]; 
  22.     if(checkpoint(s)){ 
  23.         sscanf(s, "%d.%d.%d.%d",addr, addr + 1, addr + 2, addr + 3); 
  24.         sprintf_s(tmp, sizeof(tmp), "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); 
  25.         if(strcmp(s, tmp) == 0 && checkinrange(addr)){ 
  26.             return true
  27.         } 
  28.  
  29.         //sprintf_s()  
  30.     } 
  31.     return false
  32. int main() 
  33.     char s[128] = {0}; 
  34.     int addr[4]; 
  35.      
  36.     while(scanf("%s", s) != EOF){ 
  37.         memset(addr, -1, sizeof(addr)); 
  38.         if(convertIP(s, addr)) 
  39.         { 
  40.             unsigned int result = 0; 
  41.             result = addr[0] * (0x1 << 24); 
  42.             result += addr[1] * (0x1 << 16); 
  43.             result += addr[2] * (0x1 << 8); 
  44.             result += addr[3]; 
  45.             printf("%u\n", result); 
  46.         } 
  47.         else
  48.             printf("-1\n"); 
  49.         } 
  50.  
  51.     } 
  52.  
  53.     return 0; 
Copyright © Linux教程網 All Rights Reserved