歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 網易有道面經(2013校園招聘杭州站)

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

日期:2017/2/28 15:30:48   编辑:Linux教程

上機考試:

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

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

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

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

後來回去寫的代碼如下:

  1. #include <stdio.h>
  2. #include <string.h>
  3. bool checkpoint(char *str){
  4. int npoint = 0;
  5. while(*str){
  6. (*str) == '.' ? npoint++ : npoint;
  7. if(*(str) != '.' && !((*str) <= '9' && (*str) >= '0')) return false;
  8. str++;
  9. }
  10. return npoint == 3;
  11. }
  12. bool checkinrange(int addr[4]){
  13. for(int i = 0; i < 4; i++){
  14. if(addr[i] > 255){
  15. return false;
  16. }
  17. }
  18. return true;
  19. }
  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. //sprintf_s()
  29. }
  30. return false;
  31. }
  32. int main()
  33. {
  34. char s[128] = {0};
  35. int addr[4];
  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. return 0;
  52. }
Copyright © Linux教程網 All Rights Reserved