这道题也是卡了我的一道题。。。
主要卡在没注意1<<32的时候直接变成0了。。
导致异或的时候出现错误
然后存储ip我直接用unsigned long long存的,这样方便处理,但是没有想到后面那个位运算错误。。
如果当时用数组的话可能方便很多。。
一开始的确想用数组来着。。但是觉得数组麻烦。。没想到用整数直接存会有这么一个沙雕bug
特例数据:
2
0.0.0.0
255.255.255.255
题目(vjudge):https://vjudge.net/problem/UVA-1590
代码:
//Decision's template
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<set>
using namespace std;
#define DP_maxn 16
#define maxn 1000000+10
#define INF 1000000007
#define mod 1000000007
#define mst(s,k) memset(s,k,sizeof(s))
typedef long long ll;
struct Edge{
int from,to,dist;
Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
/*-------------------------------template End--------------------------------*/
int n;
unsigned long long ip[1500];
char s[20];
int str2int(int pos_1,int pos_2){
int tmp = 0;
for(int i = pos_1;i<=pos_2;i++){
tmp *= 10;
tmp += s[i]-'0';
}
return tmp;
}
void save_ip(int now){
int len = strlen(s);
int pos = 0,now_num;
ll tmp = 0;
for(int i = 0;i<len;i++){
if(s[i] == '.'){
now_num = str2int(pos,i-1);
pos = i+1;
tmp<<=8;
tmp+=now_num;
}
}
now_num = str2int(pos,len-1);
tmp<<=8;
tmp+=now_num;
ip[now] = tmp;
}
bool check(int bits){
int now = ip[1]>>bits;
now &= 1;
for(int i = 2;i<=n;i++){
if((ip[i]>>bits&1) == now) continue;
else return false;
}
return true;
}
void print_ip(ll x){
ll tmp = x>>24;
cout<<tmp<<".";
x ^= tmp<<24;
tmp = x>>16;
cout<<tmp<<".";
x ^= tmp<<16;
tmp = x>>8;
cout<<tmp<<".";
x ^= tmp<<8;
cout<<x<<endl;
}
void solve(){
int the_n;
for(the_n = 32;the_n>=0;the_n--){
if(check(the_n)) continue;
else break;
}
the_n++;
unsigned long long t1 = 1,t2 = 1;
t1 = t1<<32;
t1--;
for(int i = 1;i<=n;i++){
t1 = min(ip[i],t1);
}
if(the_n==0) print_ip(t1);
else{
t1>>=the_n-1;
t1<<=the_n-1;
print_ip(t1);
}
t1 = 1;
t1 = t1<<32;
t1--;
t1 ^= ((t2<<the_n) - 1);
print_ip(t1);
}
int main()
{
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
while(cin>>n)
{
for(int i = 1;i<=n;i++){
cin>>s;
save_ip(i);
}
solve();
}
return 0;
}