题目大意
题目链接
假设您正在从任何设备读取代表IP地址的字节流。 您的任务是将32个字符长的序列“ 1s”和“ 0s”(位)转换为点分十进制格式。 IP地址的点分十进制格式是一次将8位分组,然后将二进制表示形式转换为十进制表示形式。 任何8位都是IP地址的有效部分。 要将二进制数转换为十进制数,请记住两个都是位置数字系统,其中二进制系统的前8个位置是:
思路
小水题,用string类从后往前依次处理每一位即可
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int main() {
int m;
string s;
char dot = '.';
cin >> m;
while (m--) {
int a[4];
cin >> s;
int ans = 0, pow = 1;
for (int i = 31; i >= 0; i--) {
ans += (s[i] - '0')*pow;
pow *= 2;
if (i % 8 == 0) a[i / 8] = ans, ans = 0, pow = 1;
}
cout << a[0] << '.' << a[1] << '.' << a[2] << '.' << a[3] << endl;
}
}