2017/9/13华为笔试--IP地址的交集

import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in  ); String[] ips1 = sc.nextLine().split("\\.");  String[] ips2 = sc.nextLine().split("\\.");  String[] ips3 = sc.nextLine().split("\\.");  String[] ips4 = sc.nextLine().split("\\.");  boolean sign = true; int i=0; for(i=0; i<4;i++) { int ip1 = Integer.parseInt(ips1[i]); int ip3 = Integer.parseInt(ips3[i]); if(ip1>ip3) { sign = false; break; } } if(sign) { int temp = 0; for(i=0; i<4;++i) { int ip2 = Integer.parseInt(ips2[i]); int ip3 = Integer.parseInt(ips3[i]); if(ip2>=ip3) { ++temp; } } if(temp == 4) { System.out.println("Overlap IP"); } else { System.out.println("No Overlap IP"); } } else{ int temp = 0; for(i=0; i<4;++i) { int ip1 = Integer.parseInt(ips1[i]); int ip4 = Integer.parseInt(ips4[i]); if(ip1<=ip4) { ++temp; } } if(temp == 4) { System.out.println("Overlap IP"); } else { System.out.println("No Overlap IP"); } } } }

全部评论
感觉不用这么麻烦,直接判断start1>end2或者start2>end1,只有一个成立,就没有交集。反之则有
点赞 回复
分享
发布于 2017-09-13 21:48
public static void main(String[] args){ Scanner input = new Scanner(System.in); String[] ips = new String[4]; for(int i=0; i<4; i++){ ips[i] = input.nextLine(); } int ip1,ip2,ip3,ip4; String[] tmp = new String[4]; for(int i=0; i<4; i++){ tmp[i] = (ips[i].split("\\."))[0]; } ip1 = Integer.parseInt(tmp[0]); ip2 = Integer.parseInt(tmp[1]); ip3 = Integer.parseInt(tmp[2]); ip4 = Integer.parseInt(tmp[3]); //不相交 if(ip1 <= ip2){ if((ip3<ip1 && ip4<ip1) || (ip3>ip2 && ip4>ip2)){ System.out.println(" No Overlap IP"); }else{ System.out.println("Overlap IP"); } }else{ if((ip3<ip2 && ip4<ip2) || (ip3>ip1 && ip4>ip1)){ System.out.println(" No Overlap IP"); }else{ System.out.println("Overlap IP"); } } }
点赞 回复
分享
发布于 2017-09-13 21:59
滴滴
校招火热招聘中
官网直投
你们说会查看代码吗?面试筛选的时候
点赞 回复
分享
发布于 2017-09-13 22:40
面试的时候 面试官会看到你写的代码
点赞 回复
分享
发布于 2017-09-13 23:39
太复杂了,直接比较字符串就行了
点赞 回复
分享
发布于 2017-09-14 01:07
最大不过4个255,转成长整形直接数值比较
点赞 回复
分享
发布于 2017-09-14 06:54
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner input = new Scanner(System.in); while(input.hasNext()){ String[] str1 = input.nextLine().split("\\.");//切割成字符串数组 String[] str2 = input.nextLine().split("\\."); String[] str3 = input.nextLine().split("\\."); String[] str4 = input.nextLine().split("\\."); long ip1 = toLong(str1); long ip2 = toLong(str2); long ipp1 = Math.min(ip1, ip2);//保证起始ip < 终止ip long ipp2 = Math.max(ip1, ip2); long ip3 = toLong(str3); long ip4 = toLong(str4); long ipp3 = Math.min(ip3, ip4); long ipp4 = Math.max(ip3, ip4); if(ipp2<ipp3||ipp1>ipp4)// System.out.println("NO Overlap IP"); else System.out.println("Overlap IP"); } input.close(); } //将ip地址转为long型 private static long toLong(String[] str) { long result = 0; for(int i=0;i<4;i++){ result += Math.pow(16, i)*Integer.valueOf(str[i-3]); } return result; } } 求助各位大神,这是我当时的代码,但是最后只能通过87.5%,想了一晚上还是没想明白,恳请赐教!
点赞 回复
分享
发布于 2017-09-14 09:08
#include <iostream> #include <string> using namespace std; int main() { string str1, str2, str3, str4; while (cin >> str1 >> str2 >> str3 >> str4) { if (str3[0] >= str1[0] && str3[0] <= str2[0] || str4[0] >= str1[0] && str4[0] <= str2[0]) cout << "Overlap IP" << endl; else cout << "No Overlap IP" << endl; } return 0; }
点赞 回复
分享
发布于 2017-09-14 09:20
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() {     char s[5][20];     long a[5][5];     unsigned long long b[5] = {0};     for(int i=0;i<4;i++)     gets(s[i]);     for(int i=0;i<4;i++)     {         int j=0;         long l=1;         long sum =0;         for(int k=strlen(s[i])-1;k>=0;k--)         {             int temp=s[i][k]-'0';             if(temp>=0&&temp<=9) {sum = sum+l*temp;l*=10;}             else if(s[i][k]=='.') {l = 1;a[i][j++]=sum;sum = 0;}         }         a[i][j] = sum;     }     int dd = 1;     for(int i=0;i<4;i++)     {         b[0] = b[0] + a[0][i]*dd;         b[1] = b[1] + a[1][i]*dd;         b[2] = b[2] + a[2][i]*dd;         b[3] = b[3] + a[3][i]*dd;         dd *= 256;     }     if(b[2]<=b[1]) cout << "Overlap IP" << endl;     else cout << "No Overlap IP" << endl;     return 0; }
点赞 回复
分享
发布于 2017-09-14 14:34
兄弟,华为可能不要重邮的研发
点赞 回复
分享
发布于 2017-09-14 14:39
for 输入:     把IP用输入流转化成4个3位数,     再把4个三位数变成一个12位数(long型) if start1<=start2<=end1 or start<=end2<=end1:     O IP else:     No O IP
点赞 回复
分享
发布于 2017-09-16 08:08

相关推荐

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