题解 | #整数与IP地址间的转换# 进制

整数与IP地址间的转换

https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea

#include <iostream>
#include <sstream>
#include <bitset>
using namespace std;

int main() {
 
    string s;
    cin >> s;
    istringstream ss(s);
    string sss;
    unsigned long long resultNum = 0;

    while (getline(ss, sss, '.')) {
        int num = stoi(sss);
        resultNum = (resultNum << 8) + num; 
    }
    cout << resultNum << '\n';

    
    cin >> s;
    unsigned long long num = stoull(s); 
    string result = "";

    for (int i = 0; i < 4; i++) {
        int byte = num & 255; 
        result = to_string(byte) + (i > 0 ? "." : "") + result; 
        num >>= 8; 
    }
    
    cout << result;
    return 0;
}

全部评论

相关推荐

牛牛不会牛泪:可以先别急着租房,去青旅,或者订个近点的宾馆待几天。先看看要做的能不能学到东西,然后看文档完不完善,写的好不好,mentor对你咋样,公司氛围啥的。情况不对赶快跑路找下家
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务