题解 | #整数与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.hasNext()) { // 注意 while 处理多个 case String s = in.next(); long ipint = 0; String[] split = s.split("\\."); for (String value : split) { ipint <<= 8; ipint += Integer.parseInt(value); } System.out.println(ipint); long[] ip = new long[4]; long n = in.nextLong(); int bit = (1 << 8) - 1; for (int i = 0; i < 4; i++) { ip[3 - i] = (n & bit); n >>= 8; } for (int i = 0; i < 4; i++) { System.out.print(ip[i]); if (i != 3) { System.out.print('.'); } } } } }