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

整数与IP地址间的转换

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] ip = sc.next().split("\\.");
        long digit = sc.nextLong();
        long one = Long.parseLong(ip[0]);
        long two = Long.parseLong(ip[1]);
        long three = Long.parseLong(ip[2]);
        long four = Long.parseLong(ip[3]);
        long ans = 0;
        ans += (one << 24) + (two << 16) + (three << 8) + four;
        System.out.println(ans);
        long one1 = digit >> 24;
        long two1 = (digit - (one1 << 24)) >> 16;
        long three1 = (digit - (one1 << 24) - (two1 << 16)) >> 8;
        long four1 = digit - (one1 << 24) - (two1 << 16) - (three1 << 8);
        System.out.printf("%d.%d.%d.%d\n", one1, two1, three1, four1);
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务