2015 Baidu STAR Program Design Competition, 2015 STAR Program Design

Source: Internet
Author: User

2015 Baidu STAR Program Design Competition, 2015 STAR Program Design

[Question link ]:Cilck here ~~

[General idea ]:

In today's world, the network is everywhere. Due to a mistake made by Xiao duxiong, as a network administrator of du, he has a large number of IP addresses on his hand, du Xiong wants to know the number of network addresses under a fixed subnet mask. The result of bitwise calculation of the subnet mask and IP address. For example:

Subnet Mask: A. B .C.D

IP Address: a. B. c. d

Network Address: (A & a). (B & B). (C & c). (D & d)

This is a simple question. You can sort and process it. Because the bool cmp function is wrong, it is still written too little after one afternoon of debugging ~~

Code:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int N=1e5+5;struct node{    int a1,a2,a3,a4;} ip[N],ans[N],res[N];bool cmp(node a,node b){    if(a.a1!=b.a1) return a.a1<=b.a1;    if(a.a2!=b.a2) return a.a2<=b.a2;    if(a.a3!=b.a3) return a.a3<=b.a3;    if(a.a4!=b.a4) return a.a4<=b.a4;}int main(){    int t,tot=1,n,m;    cin>>t;    while(t--)    {        memset(res,0,sizeof(res));        cin>>n>>m;        for(int i=1; i<=n; i++) scanf("%d.%d.%d.%d",&ip[i].a1,&ip[i].a2,&ip[i].a3,&ip[i].a4);        for(int i=1; i<=m; i++) scanf("%d.%d.%d.%d",&ans[i].a1,&ans[i].a2,&ans[i].a3,&ans[i].a4);        int k=0;        printf("Case #%d:\n",tot++);        for(int i=1; i<=m; i++)        {            int c=0,cnt=0;            for(int j=1; j<=n; j++)            {                res[k].a1=ip[j].a1&ans[i].a1;                res[k].a2=ip[j].a2&ans[i].a2;                res[k].a3=ip[j].a3&ans[i].a3;                res[k].a4=ip[j].a4&ans[i].a4;                k++;            }            sort(res+k-n,res+k,cmp);            for(int j=k-n; j<k; j++)            {                if((res[j].a1==res[j+1].a1)&&(res[j].a2==res[j+1].a2)&&(res[j].a3==res[j+1].a3)&&(res[j].a4==res[j+1].a4))                    c++;            }            printf("%d\n",n-c);        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.