网易有道面经(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; 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 下一页

相关内容