1003—IP彙總,1003彙總

來源:互聯網
上載者:User

1003—IP彙總,1003彙總
IP彙總  Accepts: 1898  Submissions: 4972 Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Others)Problem Description

當今世界,網路已經無處不在了,小度熊由於犯了錯誤,當上了度度公司的網路系統管理員,他手上有大量的 IP列表,小度熊想知道在某個固定的子網路遮罩下,有多少個網路地址。網路地址等於子網路遮罩與 IP 位址按位進行與運算後的結果,例如:

子網路遮罩:A.B.C.D

IP 位址:a.b.c.d

網路地址:(A&a).(B&b).(C&c).(D&d)

Input

第一行包含一個整數T,(1≤T≤50)代表測試資料的組數,

接下來T組測試資料。每組測試資料包含若干行,

第一行兩個正整數N(1≤N≤1000,1≤M≤50),M。接下來N行,每行一個字串,代表一個 IP 位址,

再接下來M行,每行一個字串代表子網路遮罩。IP 位址和子網路遮罩均採用 A.B.C.D的形式,其中A、B、C、D均為非負整數,且小於等於255。

Output

對於每組測試資料,輸出兩行:

第一行輸出: "Case #i:" 。i代表第i組測試資料。

第二行輸出測試資料的結果,對於每組資料中的每一個子網路遮罩,輸出在此子網路遮罩下的網路地址的數量。

Sample Input
25 2192.168.1.0192.168.1.101192.168.2.5192.168.2.7202.14.27.235255.255.255.0255.255.0.04 2127.127.0.110.134.52.0127.0.10.110.134.0.2235.235.0.01.57.16.0
Sample Output
Case #1:32Case #2:34



解題思路: 可以直接類比,關鍵用java注意逾時、
import java.util.HashSet;import java.util.Scanner;import java.util.Set;public class Main2{public static void main(String[] args){        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();      for (int p = 0; p < n; p++)      {        int i1 = sc.nextInt();        int i2 = sc.nextInt();        sc.nextLine();        String a1[] = new String[i1];        String a2[] = new String[i2];        for (int i = 0; i < a1.length; i++)        a1[i]=sc.nextLine();        for (int i = 0; i < a2.length; i++)        a2[i]=sc.nextLine();        System.out.println("Case #"+(p+1)+":");        for (int i = 0; i < i2; i++)        {        String s[] = a2[i].split("[.]");        int A = Integer.parseInt(s[0]);    int B = Integer.parseInt(s[1]);    int C = Integer.parseInt(s[2]);    int D = Integer.parseInt(s[3]);        Set<String>list=new HashSet<String>();        for (int j = 0; j < i1; j++)        {        s = a1[j].split("[.]");        int a = Integer.parseInt(s[0]);        int b = Integer.parseInt(s[1]);        int c = Integer.parseInt(s[2]);        int d = Integer.parseInt(s[3]);        StringBuffer sb = new StringBuffer();        sb.append(a&A).append(".").append(b&B).append(".").append(c&C).append(".").append(d&D);        list.add(sb.toString());        }        System.out.println(list.size());        }      }}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.