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

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String str = in.next();
            if (str.contains(".")) {
                String[] nums = str.split("\\.");
                long res = 0;
                for (int i = 0; i < 4; i++) {
                    res = res * 256 + Integer.parseInt(nums[i]);
                }
                System.out.println("" + res);
            } else {
                long num = Long.parseLong(str);
                String res = "";
                for (int i = 0; i < 4; i++) {
                    res = num % 256 + "." + res;
                    num /= 256;
                }
                System.out.println(res.substring(0, res.length() - 1));
            }
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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