Java简洁版 | #整数与IP地址间的转换#

整数与IP地址间的转换

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

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringJoiner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String ips[] = in.nextLine().split("\\.");
        BigInteger num10 = in.nextBigInteger();

        Long res1 = 0L;
        for(int i=0;i<4;i++)
            res1 += Long.valueOf(ips[3-i])<<(i*8);
        
        System.out.println(res1);

        int[] res_ips = new int[4];
        for(int i=0;i<4;i++){
            res_ips[3-i] = num10.and(new BigInteger("255")).intValue();
            num10 = num10.shiftRight(8);
        }

        StringJoiner sj = new StringJoiner(".");
        Arrays.stream(res_ips).forEach(c ->sj.add(c+""));
        System.out.println(sj);
        
    }
}

BigInteger 、 StringJoiner 、stream的使用

全部评论

相关推荐

06-18 15:03
门头沟学院 Java
至少实习看起来比去年好?问了下群里的同学和身边的同学,人均有offer。有的还有好几个大厂offer
菜鸟1973:上一年暑期也是人均大厂实习offer,结果秋招跟不招人一样,大部分都转正了
点赞 评论 收藏
分享
醉蟀:你不干有的是人干
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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