题解 | #整数与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;
}

全部评论

相关推荐

03-26 15:18
已编辑
华北水利水电大学 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务