一、题目
二、代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String ip = in.nextLine();
String ip10 = in.nextLine();
System.out.println(processIp(ip));
System.out.println(processIp10(ip10));
}
}
private static String processIp10(String ip10) {
String binaryStr = Long.toBinaryString(Long.parseLong(ip10));
if (binaryStr.length()<32) {
String s = "";
for (int i = 0; i < 32-binaryStr.length(); i++) {
s+="0";
}
binaryStr = s + binaryStr;
}
String s1 = String.valueOf(Long.parseLong(binaryStr.substring(0, 8),2));
String s2 = String.valueOf(Long.parseLong(binaryStr.substring(8, 16),2));
String s3 = String.valueOf(Long.parseLong(binaryStr.substring(16, 24),2));
String s4 = String.valueOf(Long.parseLong(binaryStr.substring(24),2));
return s1+"."+s2+"."+s3+"."+s4;
}
private static String processIp(String ip) {
String[] nums = ip.split("\\.");
StringBuilder sb = new StringBuilder();
for (String num : nums) {
String binaryStr = Integer.toBinaryString(Integer.parseInt(num));
if (binaryStr.length() < 8) {
String s = "";
for (int i = 0; i < 8-binaryStr.length(); i++) {
s+="0";
}
binaryStr = s + binaryStr;
}
sb.append(binaryStr);
}
return Long.parseLong(sb.toString(), 2)+"";
}
}