CCF-CIDR合并 java代码(仅供参考)修改
作者:互联网
题目:
试题编号: 201812-3
试题名称: CIDR合并
时间限制: 1.0s
内存限制: 512.0MB
问题描述:
样例输入
2
1
2
样例输出
1.0.0.0/8
2.0.0.0/8
样例输入
2
10/9
10.128/9
样例输出
10.0.0.0/8
样例输入
2
0/1
128/1
样例输出
0.0.0.0/0
经过思考,我运用java语言初步实现了CIDR合并的任务,一共包含两个类,一个类是ip地址,一个是主类,直接上代码
IPaddress类:
public class IPaddress { String string = null; Integer len = 0; public IPaddress(String temp, int lenth) { this.string = temp; this.len = lenth; } public String getaddress() { return this.string; } public int getlength() { return this.len; } }
接下来是MAIN类,其中DealTheIPaddress(String)方法是将输入的不是标准IP地址转换成标准的保存在iPaddresses这个list中;
FTToBinary(List<IPaddress>)是将十进制的ip地址转换成二进制的ip地址保存到binaryIPaddresses这个list中;
FSmToBigMerge(List<IPaddress>)这个方法是从小到大合并,作用题目中有描述;
SameLevelMerge(List<IPaddress>)是同级合并,作用题目中有描述;
最后是main方法,起到在控制台输入数据,调用方法的作用。
MAIN类:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class MAIN { private static List<IPaddress> iPaddresses = new ArrayList<IPaddress>(); private static List<IPaddress> binaryIPaddresses = new ArrayList<IPaddress>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Input num"); int num = scanner.nextInt(); MAIN main = new MAIN(); for (int i = 0; i < num; i++) { String iPString = scanner.next(); IPaddress temPaddress = main.DealTheIPaddress(iPString); iPaddresses.add(temPaddress); System.out.println(temPaddress.string + " len " + temPaddress.len); } Collections.sort(iPaddresses, new Comparator<Object>() {// 排序,先对ip地址大小进行判定,然后相同的条件下考虑前缀长度 @Override // 重写,因为题目中要求是先对ip地址排序,然后考虑前缀长度 public int compare(Object o1, Object o2) { IPaddress s1 = (IPaddress) o1; IPaddress s2 = (IPaddress) o2; String[] chartString1 = s1.string.split("\\.|/"); String[] chartString2 = s2.string.split("\\.|/"); int[] num1 = new int[4];// 存储将ip地址从string转换成int类型的数据 int[] num2 = new int[4]; for (int i = 0; i < 4; i++) { num1[i] = Integer.parseInt(chartString1[i]); num2[i] = Integer.parseInt(chartString2[i]); } for (int i = 0; i < 4; i++) { if (num1[i] > num2[i]) { return 1; } if (num1[i] < num2[i]) { return -1; } } if (s1.len > s2.len) { return 1; } if (s1.len < s2.len) { return -1; } return 0; } }); main.FTToBinary(iPaddresses); // 从小到大合并 main.FSmToBigMerge(iPaddresses); // 同级合并 main.SameLevelMerge(iPaddresses); for (int i = 0; i < iPaddresses.size(); i++) { System.out.println(iPaddresses.get(i).string + "/" + iPaddresses.get(i).len); } scanner.close(); } public IPaddress DealTheIPaddress(String iPString) {// 将收到的不完全的ip地址转换成标准的ip地址 IPaddress killqueen = null; String[] chartStrings = iPString.split("\\.|/"); System.out.println("length " + chartStrings.length); if (chartStrings.length < 5) {// 处理不是标准形态的ip地址 if (iPString.contains("/")) {// 包含前缀长度的非标准形态的ip地址 int length = Integer.parseInt(chartStrings[chartStrings.length - 1]); String tempString = chartStrings[0]; for (int i = 1; i < chartStrings.length - 1; i++) { tempString = tempString + "." + chartStrings[i]; } for (int j = chartStrings.length - 1; j < 4; j++) { tempString = tempString + ".0"; } killqueen = new IPaddress(tempString, length); } else {// 处理不包含前缀长度的ip地址 int lengh = chartStrings.length * 8; String tempString = chartStrings[0]; for (int i = 1; i < chartStrings.length; i++) { tempString = tempString + "." + chartStrings[i]; } for (int j = chartStrings.length; j < 4; j++) { tempString = tempString + ".0"; } killqueen = new IPaddress(tempString, lengh); } } else {// 标准形态的ip地址 String tempString = chartStrings[0]; for (int i = 1; i < chartStrings.length - 1; i++) { tempString = tempString + "." + chartStrings[i]; } killqueen = new IPaddress(tempString, Integer.parseInt(chartStrings[chartStrings.length - 1])); return killqueen; } return killqueen; } public void FTToBinary(List<IPaddress> iPaddresses) {// 将ip地址从十进制转换成二进制 for (int i = 0; i < iPaddresses.size(); i++) { IPaddress killqueen = null; String[] s1 = iPaddresses.get(i).string.split("\\.|/"); int[] num = new int[5]; String[] binStrings = new String[5]; String tempString = null; for (int j = 0; j < s1.length; j++) { num[j] = Integer.parseInt(s1[j]); binStrings[j] = Integer.toBinaryString(num[j]); } // 还需要将ip地址补全,不足8位要补齐 for (int j = 0; j < binStrings.length - 1; j++) { String tempString2 = "0"; if (binStrings[j].length() < 8) { for (int k = 1; k < 8 - binStrings[j].length(); k++) { tempString2 = tempString2 + "0"; } tempString2 = tempString2 + binStrings[j]; binStrings[j] = tempString2; } } tempString = binStrings[0]; for (int j = 1; j < binStrings.length - 1; j++) { tempString = tempString + "." + binStrings[j]; } killqueen = new IPaddress(tempString, iPaddresses.get(i).len); binaryIPaddresses.add(killqueen); } } public void FSmToBigMerge(List<IPaddress> iPaddresses) { for (int i = 0; i < binaryIPaddresses.size() - 1; i++) { boolean same = true; String[] s1 = new String[4]; String[] s2 = new String[4]; String string1 = null; String string2 = null; char[] char1 = new char[32]; char[] char2 = new char[32]; s1 = binaryIPaddresses.get(i).string.split("\\."); s2 = binaryIPaddresses.get(i + 1).string.split("\\."); string1 = s1[0]; string2 = s2[0]; for (int j = 1; j < s1.length; j++) { string1 = string1 + s1[j]; string2 = string2 + s2[j]; } for (int j = 0; j < 32; j++) { char1[j] = string1.charAt(j); char2[j] = string2.charAt(j); } for (int j = 0; j < binaryIPaddresses.get(i).len; j++) { if (char1[j] != char2[j]) { same = false; break; } } if (!same) { continue; } if (same) { binaryIPaddresses.remove(i + 1); iPaddresses.remove(i + 1); i--; continue; } } } public void SameLevelMerge(List<IPaddress> iPaddresses) { for (int i = 0; i < binaryIPaddresses.size() - 1; i++) { String[] s1 = new String[4]; String[] s2 = new String[4]; String string1 = null; String string2 = null; char[] char1 = new char[32]; char[] char2 = new char[32]; IPaddress newip = null; IPaddress newip1 = null; boolean couldmerge = true; if (binaryIPaddresses.get(i).len == binaryIPaddresses.get(i + 1).len) { s1 = binaryIPaddresses.get(i).string.split("\\."); s2 = binaryIPaddresses.get(i + 1).string.split("\\."); string1 = s1[0]; string2 = s2[0]; for (int j = 1; j < s1.length; j++) { string1 = string1 + s1[j]; string2 = string2 + s2[j]; } for (int j = 0; j < string1.length(); j++) { char1[j] = string1.charAt(j); char2[j] = string2.charAt(j); } if (char1[binaryIPaddresses.get(i).len - 1] == '0') { newip = new IPaddress(binaryIPaddresses.get(i).string, binaryIPaddresses.get(i).len - 1); newip1 = new IPaddress(iPaddresses.get(i).string, iPaddresses.get(i).getlength() - 1); } else { continue; } for (int j = 0; j < newip.len; j++) { if (char1[j] != char2[j]) { couldmerge = false; } } if (!couldmerge) { continue; } if (couldmerge) { binaryIPaddresses.remove(i + 1);// 先移除i+1,因为如果先移除i,那么后面的下标会自动-1,那么后面移除的i+1其实是原来的i+2 binaryIPaddresses.remove(i); binaryIPaddresses.add(i, newip); iPaddresses.remove(i + 1); iPaddresses.remove(i); iPaddresses.add(i, newip1); i--; } } else { continue; } } } }
标签:java,String,int,++,iPaddresses,tempString,new,CIDR,CCF 来源: https://www.cnblogs.com/yanzhao-x/p/11362482.html