题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.DateFormat; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.function.Function; import java.util.function.IntFunction; import java.util.stream.IntStream; import java.util.stream.Stream; import static java.util.Arrays.*; import static java.util.stream.Stream.*; public class Main { public static void main(String[] args) throws IOException { testTh(); } private static void testTh() throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String str; while ((str = bf.readLine()) != null) { String[] split = str.split("\\."); StringBuilder sb = new StringBuilder(); for (String string : split) { String toBinaryString = Integer.toBinaryString(Integer.parseInt(string)); if (toBinaryString.length() < 8) { while (toBinaryString.length() < 8) { toBinaryString = "0" + toBinaryString; } } sb.append(toBinaryString); } BigInteger integer = BigInteger.valueOf(Long.valueOf(sb.toString(),2)); str = bf.readLine(); if (str.length() < 10) { String s = Integer.toBinaryString(Integer.parseInt(str)); consult(s, integer); } else { BigInteger bigInteger = new BigInteger(str); String s = bigInteger.toString(2); consult(s, integer); } } } private static void consult(String s, BigInteger integer) { StringBuilder sb2 = new StringBuilder(); if (s.length() < 32) { for (int i = 0; i < 32 - s.length(); i++) { sb2.append("0"); } } sb2.append(s); String s1 = sb2.toString(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 4; i++) { Integer integer1 = Integer.valueOf(s1.substring(8 * i, 8 + 8 * i), 2); stringBuilder.append(integer1).append("."); } System.out.println(integer); System.out.println(stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString()); } }