POJ 2105 IP Address
水水更健康。
原题地址:http://poj.org/problem?id=2105
POJ 2105 IP 地址
问题描述
大家知道 IPv4 的地址是 4 段,每段是由 0 ~ 255 的整数组成的。把这 4 个段分别转换成 2 进制的表示方式,并且如果不足 8 位则在前面补上 0, 然后再依序接起来,得到一个 32 位 2 进制数。
你的任务是把 32 位 2 进制数转换成相应的 IP 地址。
样例输入
4 00000000000000000000000000000000 00000011100000001111111111111111 11001011100001001110010110000000 01010000000100000000000000000001
样例输出
0.0.0.0 3.128.255.255 203.132.229.128 80.16.0.1
解题思路
很水的题目,用上 bitset 更水。
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <cstdlib> #include <bitset> #include <iostream> #include <cstdio> using namespace std; int main(int argc, char *argv[]) { size_t n; bitset<32> bs; unsigned long ul; unsigned char (*p)[4] = (unsigned char (*)[4])&ul; cin >> n; while (n--) { cin >> bs; ul = bs.to_ulong(); printf("%d.%d.%d.%d\n", (*p)[3], (*p)[2], (*p)[1], (*p)[0]); } return EXIT_SUCCESS; } |
原创文章,转载请注明来源:http://euyuil.com/2569/poj-2105-ip-address/

这题目AC得有成就感么…闭着眼睛都能AC…数据量弱爆了…