题解 | #整数与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); String ip=in.next(); long in_dig=in.nextLong(); String[] arr=ip.split("\\."); //注意分隔符.为转义字符 long dig10=0; int len=arr.length; for(int i=0;i<len;i++){ Integer dig=Integer.valueOf(arr[i]); dig10+=(long)Math.pow(256,len-1-i)*dig; } String res_ip=""; for(int i=3;i>=0;i--){ long dig=in_dig/(int)(Math.pow(256,i)); in_dig=in_dig-(int)(Math.pow(256,i))*dig; res_ip+=Integer.toString((int)dig)+"."; } System.out.println(dig10); System.out.println(res_ip.substring(0,res_ip.length()-1)); } }