题解 | #整数与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 in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String line = in.nextLine(); String out = ""; if (line.contains(".")) { String[] len8 = line.split("\\."); System.out.println((Long.parseLong(len8[0]) << 24) + (Long.parseLong(len8[1]) << 16) + (Long.parseLong(len8[2]) << 8) + (Long.parseLong(len8[3]))); } else { Long num = Long.parseLong(line); System.out.println((num >> 24) + "." + (num >> 16 & 255) + "." + (num >> 8 & 255) + "." + (num & 255)); } } } }