题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
#include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; int pow_function(int a, int n) { int x = 1; for (int i = 0; i < n; i++) { x *= a; } return x; } int main() { string input1, input2; while (cin >> input1 >> input2) { istringstream s(input1); vector<int> v; string temp; while (getline(s, temp, '.')) { int a = stoi(temp); v.push_back(a); } long output = 0; for (int i = 0; i < v.size(); i++) { int x = pow_function(2, 24 - 8 * i); output += v[i] * x; } cout << output << endl; long n = stol(input2); vector<string> vs; int x = pow_function(2, 8); for (int i = 0; i < 4; i++) { vs.push_back(to_string(n % x)); n /= x; } for (int i = vs.size() - 1; i >= 0; i--) { cout << vs[i]; if (i != 0) { cout << '.'; } } } } // 64 位输出请用 printf("%lld")