题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
#include <iostream> #include <cmath> #include <string> using namespace std; int main() { //ch的使用又让我长见识了 char ch; string res_str; long ip_num, res; unsigned int n1, n2, n3, n4; cin >> n1 >> ch >>n2>>ch>>n3>>ch>>n4; cin>>ip_num; res = n1*pow(2, 8*3) + n2*pow(2, 8*2)+ n3*pow(2, 8) + n4; cout << res <<endl; res_str += to_string((ip_num>>24)&0xff) + "."; res_str += to_string((ip_num>>16)&0xff) + "."; res_str += to_string((ip_num>>8)&0xff) + "."; res_str += to_string(ip_num&0xff); cout << res_str << endl; } // 64 位输出请用 printf("%lld")