自己写的。无需转换为二进制。

整数与IP地址间的转换

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

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String[] ip = in.nextLine().split("\\.");
            long sum = Long.valueOf(ip[3]);
            for (int i = 2, j = 256; i >= 0; i--, j = j * 256) {
                Long temp = Long.valueOf(ip[i]);
                sum += (temp * j);
            }
            System.out.println(sum);
            long num = in.nextLong();
            String res = "";
            int j = 256 * 256 * 256;
            while (num > 255) {
                res += Long.toString(num / j);
                res += ".";
                num = num % j;
                j = j / 256;
            }
            res += Long.toString(num);
            System.out.println(res);
        }
    }
}

全部评论

相关推荐

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