国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

華為2017校招機試題第一題同網(wǎng)CS

2019-11-10 22:52:11
字體:
供稿:網(wǎng)友
描述:小明同學最近新購置了電腦,想和其他同學玩CS,但是建了局域網(wǎng)游戲之后,別人加不進來,自己也進不了別人的主機,非常苦惱。于是來請教同宿舍的“科技怪人”小犀,小犀說了句“你的ip和我們不在同一個子網(wǎng)”就閃了,小明百度了一下,搜到如下關于“子網(wǎng)掩碼”的信息:子網(wǎng)掩碼是用來判斷任意兩臺計算機的IP地址是否屬于同一子網(wǎng)絡的根據(jù)。最為簡單的理解就是兩臺計算機各自的IP地址與子網(wǎng)掩碼進行AND運算后,如果得出的結(jié)果是相同的,則說明這兩臺計算機是處于同一個子網(wǎng)里面,可以進行直接的通訊。運算演示之一:I P 地址  192.168.0.1子網(wǎng)掩碼  255.255.255.0轉(zhuǎn)化為二進制進行運算:I P 地址 11010000.10101000.00000000.00000001子網(wǎng)掩碼 11111111.11111111.11111111.00000000AND運算     11010000.10101000.00000000.00000000轉(zhuǎn)化為十進制后為:      192.168.0.0運算演示之二:I P 地址  192.168.0.254子網(wǎng)掩碼  255.255.255.0轉(zhuǎn)化為二進制進行運算:I P 地址 11010000.10101000.00000000.11111110子網(wǎng)掩碼 11111111.11111111.11111111.00000000AND運算     11010000.10101000.00000000.00000000轉(zhuǎn)化為十進制后為:      192.168.0.0運算演示之三:I P 地址  192.168.0.4子網(wǎng)掩碼  255.255.255.0轉(zhuǎn)化為二進制進行運算:I P 地址 11010000.10101000.00000000.00000100子網(wǎng)掩碼 11111111.11111111.11111111.00000000AND運算     11010000.10101000.00000000.00000000轉(zhuǎn)化為十進制后為:      192.168.0.0通過以上對三組計算機IP地址與子網(wǎng)掩碼的AND運算后,我們可以看到它運算結(jié)果是一樣的。均為192.168.0.0所以計算機就會把這三臺計算機視為是同一子網(wǎng)。小明靈機一動,打算做一個幫助大家解決局域網(wǎng)游戲問題的工具,設想如下:我知道我的電腦的IP和子網(wǎng)掩碼,又知道另外幾個同學的ip地址,通過這個工具就可以知道我可以和誰一起CS。IP尋址規(guī)則:A.網(wǎng)絡標識不能數(shù)值127開頭(在A類地址中127.0.0.1是loopback IP)B.網(wǎng)絡標識第一個字節(jié)不能是255和0C.IP每個字段不能大于255子網(wǎng)掩碼:A. 不能全部是255;B. 不能全部是0;C. 掩碼的高位(bit)必須是連續(xù)的1; 例如 : 255.255.252.0 --> 11111111.11111111.11111100.00000000 按照Bit來看1和0的位必須是連續(xù)的,而掩碼高位都是1,所以有時候會見到 錯誤 : 255.255.253.0 --> 11111111.11111111.11111101.00000000 這里就出現(xiàn)bit為1但沒有連續(xù)的情況,這種掩碼是不正確的 運行時間限制:無限制內(nèi)存限制:無限制輸入:第一行是我的電腦的IP地址第二行是我的電腦的子網(wǎng)掩碼第三行整數(shù)N,表示后面N個同學的IP地址第1個同學的IP地址......第N個同學的IP地址輸出:計算并輸出N個IP地址是否與我的電腦在同一子網(wǎng)內(nèi)。對于在同一子網(wǎng)的輸出:let's rock對于在不同子網(wǎng)的輸出:not you對于無效的聯(lián)網(wǎng)IP輸出:Invalid IP address.對于無效的子網(wǎng)掩碼:Invalid netmask address.樣例輸入:192.168.0.1255.255.255.03192.168.0.2192.168.0.200192.168.2.2樣例輸出:let's rocklet's rocknot you答案提示: 參考測試用例:測試用例1:輸入:10.123.12.7255.0.0.0310.121.234.1192.168.19.910.124.123.2輸出:let's rocknot youlet's rock測試用例2:輸入:192.168.29.1255.255.255.02127.0.0.1192.168.29.100輸出:Invalid IP address.let's rock測試用例3:輸入:10.144.240.73255.252.253.0輸出:Invalid netmask address.測試用例4:輸入:10.144.240.730.0.0.0輸出:Invalid netmask address.測試用例5:輸入:10.146.240.256輸出:Invalid IP address.測試用例6:輸入:255.148.240.1輸出:Invalid IP address.測試用例7:輸入:0.147.240.15輸出:

Invalid IP address.

程序:import java.util.Scanner;import java.util.regex.Pattern;public class First { //IP測試 public static String IPTest(String str){  String[] s=str.split("//.");//注意split中以.分割是加//  String s0=s[0];  if(s0.equals("127")||s0.equals("0")||s0.equals("255"))   return "Invalid IP Address";  for(int i=0;i<s.length;i++){   if(Integer.parseInt(s[i])>255)    return "Invalid IP Address";  }  return null; } //子網(wǎng)掩碼測試 public static String YanMaTest(String str){  if(str.equals("255.255.255.255")||str.equals("0.0.0.0"))   return "Invalid netmask address.";  String[] s=str.split("//.");  String regex="0?1+0+1+0?";  Pattern pattern=Pattern.compile(regex);  for(int i=0;i<3;i++){   s[i]=Integer.toBinaryString(Integer.parseInt(s[i]));   boolean flag=pattern.matcher(s[i]).find();   if(flag)    return "Invalid netmask address.";  }  return null; } //邏輯與 public static String AND(String str1,String str2){  String[] s1=str1.split("//.");  String[] s2=str2.split("//.");  String str="";  for(int i=0;i<s1.length;i++){   s1[i]=Integer.toBinaryString(Integer.parseInt(s1[i]));   s2[i]=Integer.toBinaryString(Integer.parseInt(s2[i]));   int count1=8-s1[i].length();   for(int j=0;j<count1;j++){    s1[i]="0"+s1[i];   }   int count2=8-s2[i].length();   for(int j=0;j<count2;j++){    s2[i]="0"+s2[i];   }   char[] ch1=s1[i].toCharArray();   char[] ch2=s2[i].toCharArray();   char[] ch=new char[8];   int count=0;   for(int j=0;j<8;j++){    if(ch1[j]=='1'&&ch2[j]=='1')     ch[j]='1';    else     ch[j]='0';   }   for(int j=0;j<8;j++){    count+=ch[j]*Math.pow(2, 7-j);   }   if(i==3)    str=str+String.valueOf(count);   else    str=str+String.valueOf(count)+".";  }  return str; }//主函數(shù): public static void main(String[] args){  System.out.PRintln("輸入:");  Scanner sc=new Scanner(System.in);  String myIP="";  String netmask="";  String otherIP="";  String result="";  int count=0;  int num=0;  while(sc.hasNext()){   count+=1;   if(count==1){    myIP=sc.next();        String str=IPTest(myIP);    if(str!=null){     System.out.println(str);     return ;    }   }   if(count==2){    netmask=sc.next();    String str=YanMaTest(netmask);    if(str!=null){     System.out.println(str);     return;    }   }   if(count==3)    num=sc.nextInt();   if(count>3){    otherIP=sc.next();    String str=IPTest(otherIP);    if(str!=null){     result+="Invalid IP address"+System.getProperty("line.separator");    }    else{     String myAnd=AND(myIP,netmask);     String otherAnd=AND(otherIP,netmask);     if(myAnd.equals(otherAnd))      result+="let's rock"+System.getProperty("line.separator");     else      result+="not you"+System.getProperty("line.separator");    }   }   if(count==3+num){    System.out.println("輸出:");    break;   }  }  System.out.println(result); } }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 夏邑县| 绥中县| 安国市| 原平市| 海兴县| 舒兰市| 福清市| 青岛市| 东辽县| 图木舒克市| 塘沽区| 冀州市| 彭水| 刚察县| 错那县| 柳江县| 沐川县| 孙吴县| 重庆市| 双鸭山市| 黄梅县| 探索| 柳林县| 阿巴嘎旗| 瑞金市| 宁武县| 张掖市| 德昌县| 莆田市| 吐鲁番市| 洛川县| 个旧市| 博爱县| 榆树市| 双桥区| 栾城县| 桦南县| 瓦房店市| 正镶白旗| 屯留县| 铜梁县|