题解 | #整数与IP地址间的转换#

整数与IP地址间的转换

https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea

#include <array>
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
int main() {
    array<long, 4> ip{0,0,0,0};
    string strin;
    long longin;
    scanf("%ld.%ld.%ld.%ld%ld",&ip[0],&ip[1],&ip[2],&ip[3],&longin);
    long out=ip[0]<<24 | ip[1]<<16 | ip[2]<<8 | ip[3];
    cout<<out<<endl;
    string outstr;
    outstr.clear();
    outstr+=to_string(longin>>24 & 0xff);
    outstr+='.';
    outstr+=to_string(longin>>16 & 0xff);
    outstr+='.';
    outstr+=to_string(longin>>8 & 0xff);
    outstr+='.';
    outstr+=to_string(longin & 0xff);
    cout<<outstr<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

LXXXXd:有点杂,想搞自动化的话没必要把法律的经历写上去
点赞 评论 收藏
分享
10-29 18:20
济南大学 Java
用微笑面对困难:他不是人事吗,怎么净特么不干人事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务