题解 | #整数与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 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String ip1 = in.next();
            String ip2 = in.next();
           String[] iparr= ip1.split("\\.");
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < iparr.length; i++) {
                StringBuilder stringBuilder1 = new StringBuilder(Integer.toBinaryString(Integer.parseInt(iparr[i])));
                if (stringBuilder1.length() < 8) {
                    int zeroCount = 8 - stringBuilder1.length();
                    for (int i1 = 0; i1 < zeroCount; i1++) {
                        stringBuilder1.insert(0, "0");
                    }

                }
                stringBuilder.append(stringBuilder1);
            }
            System.out.println(Long.parseLong(stringBuilder.toString(),2));
            String ip2binnary=Long.toBinaryString(Long.parseLong(ip2));
//            System.out.println(ip2binnary);

            int index=ip2binnary.length()/8;
            int index1=ip2binnary.toCharArray().length-8;
            int index2=ip2binnary.toCharArray().length;

//            for (int i = 0; i < index; i++) {
                StringBuilder stringBuilder2=new StringBuilder();
                for (int i = index; i >=0; i--) {
//                    if(index2==ip2binnary.toCharArray().length){
//                        index2=index2-1;
//                    }
                    if (index1 == -8) {
                        break;
                    }
                    if(index1<0){
                        index1=0;

                        StringBuilder lastBuilder=new StringBuilder(ip2binnary.substring(index1,index2));
                        for (int j=0;j<4;j++){
                            lastBuilder.insert(0,"0");
                        }
                        stringBuilder2.append(Integer.parseInt(lastBuilder.toString(), 2));
                    }else {
                        stringBuilder2.append(Integer.parseInt(ip2binnary.substring(index1, index2), 2));
                        stringBuilder2.append(".");
                        index1 = index1 - 8;
                        index2 = index2 - 8;
                    }
                }
            StringBuilder stringBuilder3 = new StringBuilder();
               String[] reverse= stringBuilder2.toString().split("\\.");
            for (int length = reverse.length-1; length >= 0; length--) {
                stringBuilder3.append(reverse[length]);
                if(length != 0){

                    stringBuilder3.append(".");
                }
            }
            System.out.println(stringBuilder3.toString());
//            }
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务