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

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 18:19
个个985的硕士闭着眼睛都有15k以上的月薪,天天嚷嚷着研究生白度读了,天天嚷嚷着反向读研了........
MMMJC:不读研22本科出去的基本都拿28k呢,你不能用25的研究生和25的本科生比然后说没反向读研,而是25研和22本比呀
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务