poj 2799 IP Networks (類比/水題)

來源:互聯網
上載者:User

標籤:algorithm   for   poj   else   font   log   cut   stdout   mem   

題目:給你一堆ip,求他們的最小網路地址和網路遮罩

思路:找到最大的ip和最小的ip,把他們轉化成二進位,從頭到尾找二進位位相同的個數,最小網路地址把後面的不同的所有二進位變成0,網路遮罩把前面的相同變成一

代碼:

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;struct node{    char k[35];} data[1005];bool cmp(node a,node b){    if(strcmp(a.k,b.k)<0) return true;    else return false;}void cut(char *str,int num){    int ans=0;    int q=8;    int flag=1;    for(int i=0; i<strlen(str); i++)    {        if(str[i]!=‘.‘)        {            ans=ans*10+str[i]-‘0‘;        }        else        {            flag++;            while(ans)            {                data[num].k[q]=ans%2+‘0‘;                ans=ans/2;                q--;            }            q=8*flag;            ans=0;        }    }    while(ans)    {        data[num].k[q]=ans%2+‘0‘;        ans=ans/2;        q--;    }    data[num].k[33]=‘\0‘;}int main(){    int m;    char k[35];    freopen("ip.in","r",stdin);    freopen("ip.out","w",stdout);    while(cin>>m)    {        for(int i=0; i<m; i++)        {            cin>>k;            memset(data[i].k,‘0‘,sizeof(data[i].k));            cut(k,i);            //cout<<data[i].k<<endl;        }        sort(data,data+m,cmp);        int n=0;        /*for(int i=0;i<m;i++)        {            cout<<data[i].k<<endl;        }*/        for(int i=1; i<=32; i++)        {            if(data[0].k[i]==data[m-1].k[i])            {                n++;            }            else break;        }        for(int i=n+1;i<=32;i++)        {            data[m-1].k[i]=‘0‘;        }        //cout<<data[m-1].k<<endl;        int up=128;        int ans=0;        for(int i=1;i<=32;i++)        {            ans=ans+up*(data[m-1].k[i]-‘0‘);            up=up/2;            if(up==0)            {                cout<<ans;                if(i!=32)                cout<<‘.‘;                up=128;                ans=0;            }        }        cout<<endl;        for(int i=1;i<=n;i++)        {            data[m-1].k[i]=‘1‘;        }        ans=0;        for(int i=1;i<=32;i++)        {            ans=ans+up*(data[m-1].k[i]-‘0‘);            up=up/2;            if(up==0)            {                cout<<ans;                if(i!=32)                cout<<‘.‘;                up=128;                ans=0;            }        }        cout<<endl;    }    return 0;}

 

poj 2799 IP Networks (類比/水題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.