实验8-2-10 IP地址转换 (20 分)
1. 题目摘自
https://pintia.cn/problem-sets/13/problems/543
2. 题目内容
一个IP地址是用四个字节(每个字节8个位)的二进制码组成。请将32位二进制码表示的IP地址转换为十进制格式表示的IP地址输出。
输入格式:
输入在一行中给出32位二进制字符串。
输出格式:
在一行中输出十进制格式的IP地址,其由4个十进制数组成(分别对应4个8位的二进制数),中间用“.”分隔开。
输入样例:
11001100100101000001010101110010
输出样例:
204.148.21.114
3. 源码参考
#include <iostream>
#include <math.h>
using namespace std;
#define len 80
int main()
{
char c[len];
int i, j;
int x, y;
cin >> c;
for(i = 0; i < 4; i++)
{
y = 0;
for(j = 8 * i; j < 8 * (i + 1); j++)
{
x = c[j] - '0';
y += x * pow(2, 7 - j % 8);
}
if(i > 0)
{
cout << ".";
}
cout << y;
}
cout << endl;
return 0;
}
网友评论