题解 | 整数与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 的区别
String a = in.next();
long result = 0;
String temp = "";
int count = 0;
for(int i = a.length()-1; i >= 0; i--){
if(a.charAt(i)!='.'){
temp = a.charAt(i) + temp;
}else{
result += Long.parseLong(temp)*Math.pow(256, count);
count++;
temp="";
}
}
result += Long.parseLong(temp)*Math.pow(256, count);
System.out.println(result);
long b = in.nextLong();
String ret = "";
while(b!=0){
long cur_w = (long)Math.pow(256, count);
ret = ret + "." + String.valueOf(b/cur_w);
b = b % cur_w;
count--;
}
System.out.println(ret.substring(1));
}
}


查看13道真题和解析