上機考試:
網易有道的篩選模式是先上機考試,然後根據上機考試選擇大概1/3參加面試。上機的平台和ACM有點類似,提交代碼然後有手動閱卷。
上機考試時隔比較久遠,不過還能想起兩個題目:
1. 給定一個點分IP地址表示,寫個程序把它轉換成相應的32位的無符號整數並輸出,如果輸入不是合法數據,就返回0.
這個題目如何利用好標准輸入輸出,其實可以很容易判斷出不合法的輸入用例,不過當時沒有想好,導致這個題目沒有AC。
後來回去寫的代碼如下:
- #include <stdio.h>
- #include <string.h>
- bool checkpoint(char *str){
-
- int npoint = 0;
- while(*str){
- (*str) == '.' ? npoint++ : npoint;
- if(*(str) != '.' && !((*str) <= '9' && (*str) >= '0')) return false;
- str++;
- }
- return npoint == 3;
- }
-
- bool checkinrange(int addr[4]){
- for(int i = 0; i < 4; i++){
- if(addr[i] > 255){
- return false;
- }
- }
- return true;
- }
- bool convertIP(char s[], int addr[4]){
- char tmp[128];
- if(checkpoint(s)){
- sscanf(s, "%d.%d.%d.%d",addr, addr + 1, addr + 2, addr + 3);
- sprintf_s(tmp, sizeof(tmp), "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
- if(strcmp(s, tmp) == 0 && checkinrange(addr)){
- return true;
- }
-
- //sprintf_s()
- }
- return false;
- }
- int main()
- {
- char s[128] = {0};
- int addr[4];
-
- while(scanf("%s", s) != EOF){
- memset(addr, -1, sizeof(addr));
- if(convertIP(s, addr))
- {
- unsigned int result = 0;
- result = addr[0] * (0x1 << 24);
- result += addr[1] * (0x1 << 16);
- result += addr[2] * (0x1 << 8);
- result += addr[3];
- printf("%u\n", result);
- }
- else{
- printf("-1\n");
- }
-
- }
-
- return 0;
- }