题解 | #整数与IP地址间的转换#

整数与IP地址间的转换

https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 3/2=1 1
 * 1/2=0 1
 * 1*2^0+1*2^1=3
 * 0*2^0+1*2^1+0*2^2+1*2^3=2+8=10
 *
 * 1010 00000000 00000011 11000001
 *
 */
public class IntegerAndIpSwitch {
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<String> params = new ArrayList<>();
        while (scanner.hasNext()) {
            if (params.size() == 1) {
                String nextLine = scanner.nextLine();
                params.add(nextLine);

                String ipAddressStr = params.get(0);
                String[] split = ipAddressStr.split("\\.");

                List<String> binaryStrList = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    String binaryString = Long.toBinaryString(Long.parseLong(split[i]));
                    if (binaryString.length() < 8) {
                        for (int j = binaryString.length(); j < 8; j++) {
                            binaryString = "0" + binaryString;
                        }
                    }
                    binaryStrList.add(binaryString);
                }
                String binaryStr = binaryStrList.stream().collect(Collectors.joining());
                char[] charArray = binaryStr.toCharArray();
                char[] copyCharArray = new char[charArray.length];
                for (int i = charArray.length - 1, j = 0; i >= 0; i--, j++) {
                    copyCharArray[j] = charArray[i];
                }
                Long result = 0L;
                for (int i = 0; i < copyCharArray.length; i++) {
                    String valueOf = String.valueOf(copyCharArray[i]);
                    if (!valueOf.equals("0")) {
                        result += count(i);
                    }
                }

                // 处理数值转ip
                String longStr = params.get(1);
                long parseLong = Long.parseLong(longStr);
                String toBinaryString = Long.toBinaryString(parseLong);
                
                int length = toBinaryString.length();
                int i = length % 8;
                int pairInt = 0;
                if (i != 0) {
                    int nearInt = length / 8;
                    pairInt = (nearInt + 1) * 8;
                }
                
                if (pairInt != 0) {
                    for (int j = length; j < pairInt; j++) {
                        toBinaryString = "0" + toBinaryString;
                    }
                }
    
                List<String> binaryStrOtherList =  new ArrayList<>();
                int loopCount = toBinaryString.length() / 8;
                int bStrLength = toBinaryString.length();
                for (int i1 = 0; i1 < loopCount; i1++) {
                    binaryStrOtherList.add(toBinaryString.substring(bStrLength - 8, bStrLength));
                    bStrLength = bStrLength - 8;
                }
    
                List<Long> nextResult = new ArrayList<>();
                for (String str : binaryStrOtherList) {
                    char[] toCharArray = str.toCharArray();
                    char[] copyCharArr = new char[toCharArray.length];
                    for (int i1 = toCharArray.length - 1, j = 0; i1 >= 0; i1--, j++) {
                        copyCharArr[j] = toCharArray[i1];
                    }
                    Long count = 0L;
                    for (int i1 = 0; i1 < copyCharArr.length; i1++) {
                        String valueOf = String.valueOf(copyCharArr[i1]);
                        if (!valueOf.equals("0")) {
                            count += count(i1);
                        }
                    }
                    nextResult.add(count);
                }
    
                System.out.println(result);
    
                StringBuilder sb = new StringBuilder();
                for (int i1 = nextResult.size() - 1; i1 >= 0; i1--) {
                    sb.append(nextResult.get(i1)).append(".");
                }
                String str = sb.toString();
                System.out.println(str.substring(0, str.length() - 1));
                
                params.clear();
            } else {
                params.add(scanner.nextLine());
            }
        }
    }
    
    private static Long count(long parseLong) {
        if (parseLong == 0) {
            return 1L;
        }
        
        return 2 * count(parseLong - 1);
    }
}

写的比较粗糙,给出的用例还错了..

全部评论

相关推荐

我见java多妩媚:大外包
点赞 评论 收藏
分享
10-25 00:32
香梨想要offer:感觉考研以后好好学 后面能乱杀,目前这简历有点难
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务