UVA 1590 IP Networks 简单模拟
2017-01-18 16:41
218 查看
WA了许多次,也不知道什么原因。。。。最后拿到官方数据对比发现原来是下溢的问题。。。。-1在无符号数下发生下溢,我仅仅考虑了n的下溢,但是没有考虑到其他的问题。。。确实不应该。
感觉题意的话.....恩,看样例可以理解题目是要干一件什么事情,但是按照题目的说法,当n=32的时候,最小的IP network岂不是0.0.0.0,
使用了一些位运算,可能导致看不清楚。以及另外一点就是cin>>和cin.get(c)的返回值都是cin,因此可以连续调用,可能有点丑了。。
这道题毕竟是一个水题,还要了这么久的时间。。假如说要数据的话,在官网可以下到http://neerc.ifmo.ru/archive/2005.html 点击Test and jury solution,假如还是不会的话,私信我好了,我发给你。
感觉题意的话.....恩,看样例可以理解题目是要干一件什么事情,但是按照题目的说法,当n=32的时候,最小的IP network岂不是0.0.0.0,
使用了一些位运算,可能导致看不清楚。以及另外一点就是cin>>和cin.get(c)的返回值都是cin,因此可以连续调用,可能有点丑了。。
这道题毕竟是一个水题,还要了这么久的时间。。假如说要数据的话,在官网可以下到http://neerc.ifmo.ru/archive/2005.html 点击Test and jury solution,假如还是不会的话,私信我好了,我发给你。
#include <iostream> #include <cstring> #include <algorithm> using namespace std; unsigned m,arr[1010],te[4],n=-1,cmp(int i,int j); char c; int main(){ ios_base::sync_with_stdio(false); while(cin>>m){ for(int i=0;i<m;++i) ((((((cin>>te[0]).get(c))>>te[1]).get(c))>>te[2]).get(c))>>te[3],arr[i]=(te[0]<<24)+(te[1]<<16)+(te[2]<<8)+te[3]; for(unsigned i=0,k,j;i<m;++i) for(j=i+1,k=cmp(i,j);j<m;k=cmp(i,++j)) if(n==-1) n=k; else if(k!=-1) n=max(n,k); if(n==-1) cout<<(arr[0]>>24)<<c<<((arr[0]&0x00ff0000)>>16)<<c<<((arr[0]&0x0000ff00)>>8)<<c<<(arr[0]&0x000000ff)<<endl<<"255.255.255.255\n"; else{ unsigned tem=arr[0]; for(int i=n;i>=0;--i) tem&=(0xffffffff^(1<<i)); cout<<(tem>>24)<<c<<((tem&0x00ff0000)>>16)<<c<<((tem&0x0000ff00)>>8)<<c<<(tem&0x000000ff)<<endl; tem=0xffffffff; for(int i=n;i>=0;--i) tem^=(1<<i); cout<<(tem>>24)<<c<<((tem&0x00ff0000)>>16)<<c<<((tem&0x0000ff00)>>8)<<c<<(tem&0x000000ff)<<endl; } n=-1; } return 0; } unsigned cmp(int i,int j){ unsigned tem=arr[i]^arr[j],ans=-1; for(unsigned k=0;tem;++k,tem>>=1) if(tem&1) ans=k; return ans; }
相关文章推荐
- 简单模拟 UVa 10267 - Graphical Editor
- UVA 340 Master-Mind Hints 猜数字提示的游戏 简单模拟
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- UVa 100 简单模拟
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- UVA 299 Train Swapping 列车交换 冒泡排序简单模拟
- UVa 1590 IP网络(简单位运算)
- UVALive 2052 Number Steps【简单模拟】水题
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- Uva 101 - The Blocks Problem//简单模拟
- UVA - 10716 - Evil Straw Warts Live (简单模拟)
- UVA 1586 分子量(简单模拟计算)
- UVALive - 7000 Present Problem 简单模拟
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- UVa10118 - Free Candies dp+简单模拟
- UVA 10161-Ant on a Chessboard 简单模拟
- 紫书章四 Spreadsheet Tracking UVA - 512(单元格去哪了,模拟结果更简单))
- UVa 602 简单模拟