记录编号 |
31351 |
评测结果 |
AAAAAAAAAA |
题目名称 |
IP网络管理员 |
最终得分 |
100 |
用户昵称 |
Makazeu |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.024 s |
提交时间 |
2011-11-02 12:23:08 |
内存使用 |
0.34 MiB |
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
class IPAddress
{
public:
int b[5];
char B[5][10];
IPAddress()
{
for (int i=1;i<=4;i++)
memset(B[i],'\0',sizeof(B[i]));
}
}IP[1001];
int N;
int Same=0;
char Sam[5][10];
int power(int base,int index)
{
int l=1;
for (int i=1;i<=index;i++)
l*=base;
return l;
}
void num2str(int num,char *s,int base)
{
char map[] = {"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char dst[64];
int i=0,n;
while(num)
{
dst[i++]=map[num%base];
num/=base;
}
dst[i]='\0';
n=i;
for(i=n-1;i>=0;--i)
{
s[n-1-i]=dst[i];
}
s[n]='\0';
}
void init()
{
for (int i=1;i<=4;i++)
memset(Sam[i],'\0',sizeof(Sam[i]));
scanf("%d\n",&N);
char tmp[10];
for (int i=1;i<=N;i++)
{
scanf("%d.%d.%d.%d\n",&IP[i].b[1],&IP[i].b[2],&IP[i].b[3],&IP[i].b[4]);
for (int j=1;j<=4;j++)
{
memset(tmp,'\0',sizeof(tmp));
num2str(IP[i].b[j],tmp,2);
int len=strlen(tmp);
int top=0;
for (int k=0;k<8-len;k++)
{
IP[i].B[j][k]='0';
top++;
}
for (int k=0;k<len;k++)
{
IP[i].B[j][top]=tmp[k];
top++;
}
}
}
}
void print()
{
for (int i=1;i<=N;i++)
{
for (int j=1;j<=4;j++)
{
for (int k=0;k<8;k++)
{
cout<<IP[i].B[j][k];
}
cout<<" ";
}
cout<<endl;
}
cout<<endl;
}
void work()
{
for (int k=1;k<=4;k++)
{
for (int i=0;i<8;i++)
{
//bool ok=true;
//int tmp=IP[1].B[k][i];
for (int j=2;j<=N;j++)
{
if(IP[j].B[k][i]!=IP[1].B[k][i])
return;
}
Sam[k][i]=IP[1].B[k][i];
Same++;
}
}
}
void computer()
{
/*Subnet Mask*/
char SM[5][10];
for (int i=1;i<=4;i++)
{
for (int j=0;j<8;j++)
{
SM[i][j]=Sam[i][j];
if(SM[i][j]!='\0')
SM[i][j]='1';
else
SM[i][j]='0';
}
}
/*IP Address*/
char AD[5][10];
for (int i=1;i<=4;i++)
{
for (int j=0;j<8;j++)
{
AD[i][j]=Sam[i][j];
if(AD[i][j]=='\0')
AD[i][j]='0';
}
}
/*
for (int i=1;i<=4;i++)
{
for (int j=0;j<8;j++)
cout<<AD[i][j];
cout<<" ";
}
cout<<endl;
for (int i=1;i<=4;i++)
{
for (int j=0;j<8;j++)
cout<<SM[i][j];
cout<<" ";
}
cout<<endl;
*/
//int Num[5];
for (int i=1;i<=4;i++)
{
int T=0;
for (int j=0;j<8;j++)
{
T=T+(AD[i][j]-'0')*power(2,7-j);
}
//Num[i]=T;
if(i<4)
cout<<T<<".";
else
cout<<T<<endl;
}
for (int i=1;i<=4;i++)
{
int T=0;
for (int j=0;j<8;j++)
{
T=T+(SM[i][j]-'0')*power(2,7-j);
}
//Num[i]=T;
if(i<4)
cout<<T<<".";
else
cout<<T<<endl;
}
}
int main()
{
freopen("networkip.in","r",stdin);
freopen("networkip.out","w",stdout);
init();
work();
//print();
computer();
return 0;
}