/* 思路: 1. ip转整数 a. 以 '.' 来分割ip字符串 b. 分割后的字符串转成数字存在vector<size_t>数组中 c. 通过 左移 与 或运算 得到整数 2. 整数转ip a. 通过对整数进行 与运算 和 右移, 得到四个数字 b. 通过 to_string 把四个数字转换为字符串 c. 拼接ip字符串 */ #include <iostream> #include <string> #include <vector> using namespace std; // 以 '.' 来分割ip字符串 size_t ipToStr(string str){ vector<size_t> v; int pos = str.find_first_of('.'); string tmp = str.substr(0, pos); v.push_back(stoi(tmp)); str = str.substr(pos+1); pos = str.find_first_of('.'); v.push_back(stoi(str.substr(0, pos))); str = str.substr(pos+1); pos = str.find_first_of('.'); v.push_back(stoi(str.substr(0, pos))); str = str.substr(pos+1); pos = str.find_first_of('.'); v.push_back(stoi(str.substr(0, pos))); size_t ipInt = v[0] << 24 | v[1] << 16 | v[2] << 8 | v[3]; return ipInt; ; } string numToIP(size_t str){ string ans; size_t ipNum = (str); ans += to_string((ipNum >> 24) & 0xff); ans.append(1, '.'); ans += to_string((ipNum >> 16) & 0xff); ans.append(1, '.'); ans += to_string((ipNum >> 8) & 0xff); ans.append(1, '.'); ans += to_string((ipNum >> 0) & 0xff); return ans; } int main() { string ip; size_t num; cin >> ip; cin >> num; cout << ipToStr(ip) << endl; cout << numToIP(num) << endl; return 0; } // 64 位输出请用 printf("%lld")