保持着也不知道什么情怀,觉得到现在才能发出来。这道题做完之后看了其他人的代码,然后再看我的,不得不说,真是幼稚的很,尤其是输入这一块,都什么跟什么啊。
但相较于之前来说,不像以前慌张了,学会先思考再去写代码,明白了函数的一些用处与含义。我知道一个大四的老狗说这种话倒还真是够没面子的,但希望自己每天都在成长。你眼里的小草,我心中的大树。
IP聚合
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
2
5 2
192.168.1.0
192.168.1.101
192.168.2.5
192.168.2.7
202.14.27.235
255.255.255.0
255.255.0.0
4 2
127.127.0.1
10.134.52.0
127.0.10.1
10.134.0.2
235.235.0.0
1.57.16.0
Sample Output
Case #1:
3
2
Case #2:
3
4
代码:
- #include <iostream>
- #include <vector>
- using namespace std;
- int ip[1005][5];
- int yanma[55][5];
- int M,N;
- int result[1005][5];
- vector<int>xiangdeng;
- void yihuo(int count)
- {
- int i;
- for(i=1;i<=M; i ++)
- {
- int j;
- for(j=1;j<=4;j++)
- {
- result[i][j]=(ip[i][j]&yanma[count][j]);
- }
- }
- }
- bool panduan(int a)
- {
- int count;
- for(count=0;count<xiangdeng.size();count++)
- {
- if(xiangdeng[count]==a)
- {
- return false;
- }
- }
- return true;
- }
- void butong()
- {
- int i,j;
- int result_real=0;
- int neng[1005];
- for(i=1;i<=1000;i++)
- {
- neng[i]=1;
- }
- for(i=1;i<M;i++)
- {
- int m;
- if(neng[i]==1)
- {
- for(m=i+1;m<=M;m++)
- {
- int flag =1;
- for(j=1;j<=4;j++)
- {
- if(result[i][j]!=result[m][j])
- {
- if(flag==1)
- {
- flag=0;
- }
- }
- }
- if(flag == 1)
- {
- neng[m]=0;
- }
- }
- }
- }
- for(i=1;i<=M;i++)
- {
- if(neng[i])
- result_real++;
- }
- cout<<result_real<<endl;
- }
- int main()
- {
- int count,countone;
- cin>>count;
- for(countone=1;countone<=count;countone++)
- {
- cin>>M>>N;
- int count1;
- for(count1=1;count1<=M;count1++)
- {
- int count2;
- for(count2=1; count2<=4;count2++)
- {
- cin>>ip[count1][count2];
- if(count2!=4)
- {
- char i;
- cin>>i;
- }
- }
- }
- for(count1=1;count1<=N;count1++)
- {
- int count2;
- for(count2=1; count2<=4;count2++)
- {
- cin>>yanma[count1][count2];
- if(count2!=4)
- {
- char i;
- cin>>i;
- }
- }
- }
- cout<<"Case #"<<countone<<":"<<endl;
- for(count1=1;count1<=N;count1++)
- {
- yihuo(count1);
- butong();
- }
- }
- return 0;
- }
看了其他人的AC代码,首先你看人家的输入scanf(“%d.%d.%d.%d”),另外一点就是完全可以把IP地址弄成一个数去比较,<<8 <<16 <<24即可。而不用像我一直在想如何在多个数组中找,太麻烦了。
最后的感受就是一种思路不行的时候,换一个别的可能就会OK。一开始只想找差别,各种wrong。最后的想法是找相同的,统计不同的就行了。
但总之,这是我第一次比赛中的AC,不管题多简单,自己做得又是有多麻烦,希望这是起点。