1590 - IP Networks

巩固了下“局部变量传参给函数并要在函数中修改其值并返回调用处使用修改的值  需传指针”

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<cctype>
 8 #include<vector>
 9 #include<queue>
10 #include<map>
11 #include<set>
12 #define eps 10e-6
13 
14 using namespace std;
15 
16 typedef long long ll;
17 
18 int ip[1005][35];
19 
20 void solve(int t,int *sum,int id)
21 {
22     int tmp[10],p = 0,w = t;
23     while(t>0)
24     {
25         tmp[p++] = t%2;
26         t/=2;
27     }
28     while(p<8) tmp[p++] = 0;
29     for(int i=7;i>=0;i--)
30         ip[id][(*sum)++] = tmp[i];
31 }
32 
33 int cal(int st,int len)
34 {
35     int ret = 0;
36     for(int i=st;i<st+8;i++)
37     {
38         if(i<len) ret = ret*2+ip[0][i];
39         else ret = ret*2;
40     }
41     return ret;
42 }
43 int calb(int st,int len)
44 {
45     int ret = 0;
46     for(int i=st;i<st+8;i++)
47     {
48         if(i<len) ret = ret*2+1;
49         else ret = ret*2;
50     }
51     return ret;
52 }
53 
54 int main()
55 {
56     int n;
57     while(~scanf("%d",&n))
58     {
59         int x[4];
60         for(int i=0;i<n;i++)
61         {
62             scanf("%d.%d.%d.%d",&x[0],&x[1],&x[2],&x[3]);
63             int cnt = 0;
64             for(int j=0;j<4;j++)
65                 solve(x[j],&cnt,i);
66         }
67         int len = 32;
68         for(int i=1;i<n;i++)
69         {
70             int s = 0;
71             for(int j=0;j<32;j++)
72             {
73                 if(ip[i][j]==ip[0][j])
74                     s++;
75                 else break;
76             }
77             len = min(len,s);
78         }
79         for(int i=0;i<24;i+=8)
80             printf("%d.",cal(i,len));
81         printf("%d\n",cal(24,len));
82         for(int i=0;i<24;i+=8)
83             printf("%d.",calb(i,len));
84         printf("%d\n",calb(24,len));
85     }
86     return 0;
87 }
View Code

 

posted @ 2015-10-24 20:17  fukan  阅读(196)  评论(0编辑  收藏  举报