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));
            }
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 15:39
点赞 评论 收藏
分享
05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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