笔试编程题-小米-运维工程师
发布于 2017-09-18 20:36 2527 次浏览 0 赞 来自 试题交流  

2 条回复
  1. 合法的IPV4

#include <stdio.h>
#include <string.h>

int main()
{
    char str[32],str_t[32];
    int a,b,c,d;
    while(gets(str))
    {
        //printf("%c\n",str[strlen(str)-1]);
        if(sscanf(str, "%d.%d.%d.%d",&a,&b,&c,&d)==4 &&   a>0   &&   a<=255 &&   b>=0   &&   b<=255 &&   c>=0   &&   c<=255 &&   d>=0   &&   d<=255)
        {
            sprintf(str_t, "%d.%d.%d.%d",a,b,c,d);
            if(strcmp(str_t,str))
            {
                printf("0\n");
            }
            else
            {
                printf("1\n");
            }
        }
        else
        {
            printf("0\n");
        }
    }
    return 0;
}

2.失落的IP

#include<iostream>
#include<vector>
#include<string>
using namespace std;

// LeetCode, Restore IP Addresses
/**
* [@brief](/user/brief) 解析字符串
* [@param](/user/param)[in] s 字符串,输入数据
* [@param](/user/param)[in] startIndex 从s 的哪里开始
* [@param](/user/param)[in] step 当前步骤编号,从0 开始编号,取值为0,1,2,3,4 表示结束了
* [@param](/user/param)[out] intermediate 当前解析出来的中间结果
* [@param](/user/param)[out] result 存放所有可能的IP 地址
* [@return](/user/return) 无
*/
void dfs(string s, int start, int step, string ip,
    vector<string> &result) {
    if (s.size() - start > (4 - step) * 3)
        return; // 非法结果,剪枝
    if (s.size() - start < (4 - step))
        return; // 非法结果,剪枝
    if (start == s.size() && step == 4) { // 找到一个合法解
        ip.resize(ip.size() - 1);
        result.push_back(ip);
        return;
    }
    int num = 0;
    for (int i = start; i < start + 3; i++) {
        num = num * 10 + (s[i] - '0');
        if (num <= 255) { // 当前结点合法,则继续往下递归
            ip += s[i];
            dfs(s, i + 1, step + 1, ip + '.', result);
        }
        if (num == 0) break; // 不允许前缀0,但允许单个0
    }
}
vector<string> restoreIpAddresses(string s) {
    vector<string> result;
    string ip; // 存放中间结果
    dfs(s, 0, 0, ip, result);
    return result;
}

int main(){
    string S;
    cin>>S;
    vector<string> Vec;
    Vec=restoreIpAddresses(S);
    for(int i=0;i<Vec.size();i++)
        cout<<Vec[i]<<endl;
    return 0;
}

3.ip地址白名单

#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;


int main(){
    vector<string> vec;
    int i=0;
    char id[2],ip[32];
    string str;
    while(cin>>str){
        if(str.compare("end")==0)
            break;
        int pos=str.find(":");
        string id=str.substr(0,pos);
        string ip=str.substr(pos+1,str.size());
        if(id[0]=='i'){
            vec.push_back(ip);
            cout<<"ok"<<endl;
        }
        else if(id[0]=='s'){
            vector<string>::iterator it;
            for(it=vec.begin();it!=vec.end();it++){
                if(it->compare(ip)==0){
                    cout << "true" << endl;
                    break;
                }
            }
            if(it==vec.end())
                cout << "false" << endl;
        }
        else if(id[0]=='d'){
            vector<string>::iterator it;
            for(it=vec.begin();it!=vec.end();it++){
                if(it->compare(ip)==0){
                    vec.erase(it);
                    cout << "ok" << endl;
                    break;
                }
            }
        }

        i++;
    }
    return 0;
}


2017-09-18 22:04
2

小米电话面试:自我介绍,malloc,i/o,反向代理。

2017-09-19 17:55
添加回复
回到顶部