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

整数与IP地址间的转换

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

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

long long st2i(string s){
    long long x=0;
    for(char ch:s)
        x=10*x+ch-'0';
    return x;
}

int main() {
    string s1,s2;
    cin >> s1 >> s2;
    long long x1=0;
    int l=0;
    int p=3;
    for(int r=0;r<=s1.size();++r){
        if(s1[r]=='.'||r==s1.size()){
            string temp=s1.substr(l,r-l);
            x1+=st2i(temp)*pow(256, p--);
            l=r+1;
        }
    }
    long long y=st2i(s2);
    vector<int> a(4);
    p=3;
    while(y){
        a[p--]=y%256;
        y/=256;
    }

    cout << x1<<endl;
    cout << a[0] << '.'<< a[1] << '.'<< a[2] << '.'<< a[3];
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

01-15 13:52
已编辑
河南大学 Java
六年要多久:标准头像,不吃香菜😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务