1590 IP Networks java

udebug上的测试都通过了,提交uva,wrong answer,不知道哪错了

package com.uva;

import java.math.BigInteger;
import java.util.Scanner;

public class IpNetworks {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()){
            int m = Integer.parseInt(sc.nextLine());
            if(m==1){
                System.out.println(sc.nextLine());
                System.out.println("255.255.255.255");
            }else{
                String[] strArr = new String[m];
                for (int i = 0; i < m; i++) {
                    String[] strArr1 = sc.nextLine().split("\\.");
                    StringBuilder sb1 = new StringBuilder();
                    for (int i1 = 0; i1 < strArr1.length; i1++) {
                        sb1.append(toBinary(strArr1[i1]));
                    }
                    strArr[i] = sb1.toString();
                }

                String[] rst = getNum(strArr);
                for (int i = 0; i < 2; i++) {
                    System.out.println(rst[i]);
                }
            }

        }
    }

    /**
     * 2进制字符串转换成十进制字符串
     * @param binarySource 2进制字符串
     * @return 转换成的10进制字符串
     */
    public static String toDecimal(String binarySource){
        BigInteger bi = new BigInteger(binarySource, 2);
        return bi.toString();
    }

    public static String toBinary(String decimalSource){
        int n = Integer.parseInt(decimalSource);
        String s = Integer.toBinaryString(n);
        return String.format("%08d",Integer.parseInt(s));
    }
    public static String[] getNum(String[] ipArrs){
        int n = ipArrs.length;
        int index=0;
        w:for (int i = 0; i < 32; i++) {
            char c1 = ipArrs[0].charAt(i);
            for (int j = 1; j < n; j++) {
                if(c1!=ipArrs[j].charAt(i)){
                    index = i;
                    break w;
                }
            }
        }
        //创建子网掩码
        StringBuilder sb = new StringBuilder();
        StringBuilder sbMin = new StringBuilder();
        for (int i = 0; i < 32; i++) {
            if(i<index) {
                sb.append(1);
                sbMin.append(ipArrs[0].charAt(i));
            }else{
                sb.append(0);
                sbMin.append(0);
            }
        }
        StringBuilder sb2 = new StringBuilder();
        String str = sb.toString();
        StringBuilder sb3 = new StringBuilder();
        String str3 = sbMin.toString();
        for (int i = 0; i < 32; i+=8) {
            int i2 = i+8;
            sb2.append(toDecimal(str.substring(i,i2)));
            sb3.append(toDecimal(str3.substring(i,i2)));
            if(i!=24){
                sb2.append(".");
                sb3.append(".");
            }
        }
        String[] rst = new String[2];
        rst[0] = sb3.toString();
        rst[1] = sb2.toString();
        return rst;
    }
}
/*
Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he decided
to group all those IP addresses into the smallest possible IP network.
Each IP address is a 4-byte number that is written byte-by-byte in a decimal dot-separated notation
“byte0.byte1.byte2.byte3” (quotes are added for clarity). Each byte is written as a decimal number
from 0 to 255 (inclusive) without extra leading zeroes.
IP network is described by two 4-byte numbers — network address and network mask. Both network
address and network mask are written in the same notation as IP addresses.
In order to understand the meaning of network address and network mask you have to consider
their binary representation. Binary representation of IP address, network address, and network mask
consists of 32 bits: 8 bits for byte0 (most significant to least significant), followed by 8 bits for byte1,
followed by 8 bits for byte2, and followed by 8 bits for byte3.
IP network contains a range of 2n IP addresses where 0 ≤ n ≤ 32. Network mask always has
32 − n first bits set to one, and n last bits set to zero in its binary representation. Network address has
arbitrary 32 − n first bits, and n last bits set to zero in its binary representation. IP network contains
all IP addresses whose 32 − n first bits are equal to 32 − n first bits of network address with arbitrary
n last bits. We say that one IP network is smaller than the other IP network if it contains fewer IP
addresses.
For example, IP network with network address 194.85.160.176 and network mask 255.255.255.248
contains 8 IP addresses from 194.85.160.176 to 194.85.160.183 (inclusive).

Input

The input file will contain several test cases, each of them as described below.
The first line of the input file contains a single integer number m (1 ≤ m ≤ 1000). The following m
lines contain IP addresses, one address on a line. Each IP address may appear more than once in the
input file.

Output

For each test case, write to the output file two lines that describe the smallest possible IP network that
contains all IP addresses from the input file. Write network address on the first line and network mask
on the second line.

Sample Input
3
194.85.160.177
194.85.160.183
194.85.160.178

Sample Output
194.85.160.176
255.255.255.248

 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值