题解 | #整数与IP地址间的转换#按照输入格式粗暴分类
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
#include <bitset> #include <iostream> #include <string> using namespace std; int main() { string str1; string str2; int num=0; long ans_out1=0; getline(cin, str1); getline(cin, str2); string temp_string=str1; string string_out; int total_len=str1.size(); for (int temp=0; temp<4; temp++) { string temp_answer; total_len=temp_string.size(); int temp_num=temp_string.find('.'); temp_answer=temp_string.substr(0,temp_num); temp_string=temp_string.substr(temp_num+1,total_len-temp_num-1); double num_for_answer=1; for(int i=3;i>temp;i--) num_for_answer=num_for_answer*256; ans_out1=ans_out1+stoi(temp_answer)*num_for_answer; } cout<<ans_out1<<endl; long total_num=stol(str2); long temp_num2=total_num; for (int temp=0; temp<4; temp++) { int answer_num=0; long num_for_answer=1; if(temp<3) { for(int i=3;i>temp;i--) num_for_answer=num_for_answer*256; } answer_num=temp_num2/num_for_answer; string_out=string_out+to_string(answer_num); if(temp!=3) string_out=string_out+"."; temp_num2=temp_num2%num_for_answer; } cout<<string_out<<endl; } // 64 位输出请用 printf("%lld")