题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String a = sc.nextLine(); // IP处理 String[] arr = s.split("\\."); long result1 = 0; for (int i = 0; i < arr.length; i++) { result1 = result1 * 256 + Integer.parseInt(arr[i]); } System.out.println(result1); // 整数处理 long ipv4 = Long.parseLong(a); String result2 = ""; for (int i = 0; i < 4; i++) { result2 = ipv4 % 256 + "." + result2; ipv4 /= 256; } result2 = result2.substring(0, result2.length() - 1); System.out.println(result2); } }