题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
#include<iostream> #include<sstream> #include<vector> using namespace std; long getNum(string& s){ vector<long> nums; long i=0,n=s.size(); while(i<n){ long start=i; while(i<n&&s[i]!='.') i++; nums.push_back(stoi(s.substr(start,i-start))); i++; } long res=0; for(long i=0;i<4;i++){ res=res*256+nums[i]; } return res; } string getIp(long num){ string res; res='.'+to_string(num%256)+res; num/=256; res='.'+to_string(num%256)+res; num/=256; res='.'+to_string(num%256)+res; num/=256; res=to_string(num)+res; return res; } int main(){ string s; long num; getline(cin,s); cin>>num; long res=getNum(s); string ip=getIp(num); cout<<res<<endl; cout<<ip<<endl; }