习题4-5
https://vjudge.net/problem/UVA-1590
这题其实不如自己写函数转换进制来的实在
bitset类
int a=99;
bitset<8> bint(a);//把a转化为8为二进制存放在bint中
cout<<bint.to_ulong()<<endl;//输出把bint转换为十进制后的数
#include<iostream>
#include<string>
#include<sstream>
#include<bitset>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
void P(string s) {
bitset<8> bint;
stringstream S(string(s, 0,8));
S >> bint;
cout << bint.to_ulong();
S.str(string(s, 8, 8));
S >> bint;
cout <<"."<< bint.to_ulong();
S.str(string(s, 16, 8));
S >> bint;
cout << "." << bint.to_ulong();
S.str(string(s, 24, 8));
S >> bint;
cout << "." << bint.to_ulong() <<endl;
}
int main() {
#ifdef _DEBUG
//freopen("in", "rb", stdin);
//freopen("out", "wb", stdout);
#endif // _DEBUG
int n;
while (cin >> n) {
string s;
string sta[1002];
int top = -1;
while (n--) {
cin >> s;
for (int i = 0; i < s.size(); ++i)
if (s[i] == '.') s[i] = ' ';
int a[4];
sscanf(s.c_str(), "%d %d %d %d", a, a + 1, a + 2, a + 3);
s = "";
for (int i = 0; i < 4; ++i) {
bitset<8> bs(a[i]);
stringstream S;
S << bs;
s += string(8 - S.str().size(), '0') + S.str();
}
sta[++top] = s;
}
int i = 0;
bool _end = false;
for (; i < 32; ++i) {
for (int j = 1; j <= top; ++j)
if (sta[j][i] != sta[0][i]) {
_end = true; break;
}
if (_end) break;
}
s = string(i, '1') + string(32 - i, '0');
for (; i < 32; ++i)
sta[0][i] = '0';
P(sta[0]);
P(s);
}
}