IP Address


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format.
A dotted decimal format for an IP address is form by grouping 8 bits at a time and converting the binary representation to decimal representation. Any 8 bits is a valid part of an IP address. To convert binary numbers to decimal numbers remember that both
are positional numerical systems, where the first 8 positions of the binary systems are:

27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1

Input

The input will have a number N (1 <= N <= 9) in its first line representing the number of streams to convert. N lines will follow.

Output

The output must have N lines with a doted decimal IP address. A dotted decimal IP address is formed by grouping 8 bit at the time and converting the binary representation to decimal representation.

Sample Input

4

00000000000000000000000000000000

00000011100000001111111111111111

11001011100001001110010110000000

01010000000100000000000000000001

Sample Output

0.0.0.0

3.128.255.255

203.132.229.128

80.16.0.1

——————————————————————————————————

给出一个32位二进制,化成点分十进制的IP地址形式

8位一处理即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <functional>
#include <bitset>
#include <string> using namespace std; #define LL long long
#define INF 0x3f3f3f3f int main()
{
int T;
char s[1000];
int a[10];
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
a[8]=1;
for(int i=7;i>0;i--)
a[i]=2*a[i+1];
int a1=0,a2=0,a3=0,a4=0;
int cnt;
cnt=1;
for(int i=0;i<8;i++)
{
a1+=(s[i]-'0')*a[cnt++];
}
cnt=1;
for(int i=8;i<16;i++)
{
a2+=(s[i]-'0')*a[cnt++];
}
cnt=1;
for(int i=16;i<24;i++)
{
a3+=(s[i]-'0')*a[cnt++];
}
cnt=1;
for(int i=24;i<32;i++)
{
a4+=(s[i]-'0')*a[cnt++];
}
printf("%d.%d.%d.%d\n",a1,a2,a3,a4); }
return 0;
}

ZOJ2482 IP Address 2017-04-18 23:11 44人阅读 评论(0) 收藏的更多相关文章

  1. PAT甲 1029. Median (25) 2016-09-09 23:11 27人阅读 评论(0) 收藏

    1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...

  2. POJ3281 Dining 2017-02-11 23:02 44人阅读 评论(0) 收藏

    Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and dri ...

  3. RedHat Enterprise Linux 6.4使用Centos 6 的yum源 分类: 服务器搭建 Nginx 2015-07-14 14:11 5人阅读 评论(0) 收藏

    转载自:http://blog.sina.com.cn/s/blog_50f908410101cto6.html 思路:卸载redhat自带yum,然后下载centos的yum,安装后修改配置文件 1 ...

  4. Codeforces816A Karen and Morning 2017-06-27 15:11 43人阅读 评论(0) 收藏

    A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

  5. highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  6. C#期末大作业 消消乐 2017-06-01 18:11 275人阅读 评论(0) 收藏

    邻近期末,忙于刷题之余意识到期末大作业来不及了,匆匆赶下了作业,虽说做的很是粗糙,但完全原创的 下载链接 https://pan.baidu.com/s/1cCNLr4 大体的做大约3天完成了: 第一 ...

  7. 用Apache Ivy实现项目里的依赖管理 分类: C_OHTERS 2014-07-06 18:11 564人阅读 评论(0) 收藏

    Apache Ivy是一个管理项目依赖的工具. 它与Maven  Apache Maven 构建管理和项目管理工具已经吸引了 Java 开发人员的注意.Maven 引入了 JAR 文件公共存储库的概念 ...

  8. Hdu2204 Eddy's爱好 2017-06-27 16:11 43人阅读 评论(0) 收藏

    Eddy's爱好 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Subm ...

  9. 动态链接库(DLL) 分类: c/c++ 2015-01-04 23:30 423人阅读 评论(0) 收藏

    动态链接库:我们经常把常用的代码制作成一个可执行模块供其他可执行文件调用,这样的模块称为链接库,分为动态链接库和静态链接库. 对于静态链接库,LIB包含具体实现代码且会被包含进EXE中,导致文件过大, ...

随机推荐

  1. one by one 项目 part 5

    问题汇总 一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分析:这说明“localhos ...

  2. MVC3 发布到IIS 7.5

    1.应用程序池采用集成模式(建议),.NET Framework版本为: .NET Framework4.0.30319. 2.确保ASP.NET MVC3已安装好,然后检查站点的处理程序映射,看是否 ...

  3. 吴裕雄 实战PYTHON编程(9)

    import cv2 cv2.namedWindow("ShowImage1")cv2.namedWindow("ShowImage2")image1 = cv ...

  4. Electron 的解释, 什么是Electron

    https://wizardforcel.gitbooks.io/electron-doc/content/development/build-instructions-windows.html

  5. poj3186(区间DP)

    题目链接:http://poj.org/problem?id=3186 思路: 区间DP,给treat编号为1..n,状态很明显是上界i和下界j,dp[i][j]表示从下标i到下标j之间数据的最大价值 ...

  6. 通过阿里OSS文件服务返回的URL获取文件流下载

    我们都知道将文件上传到阿里的OSS文件服务上后,可以通过generatePresignedUrl(bucketName, key, expiration)方法获取该文件的防问路径,但是当我们知道该文件 ...

  7. 全国省市区数据库SQL(有可能不是最新的)

    百度云下载地址:https://pan.baidu.com/s/1lStN7tYpwOtpC-r3G2X2sw

  8. 88. Merge Sorted Array 后插

    合并两个排序的整数数组A和B变成一个新的数组.给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B ...

  9. AasyncTask中执行execute()还是executeOnExecutor()

    executeOnExecutor()api 11 才出现的 并行的 效率比execute()高因为execute()是串行的 import android.app.Activity; import ...

  10. 五步打造APP节日主题设计:以Lofter新年图标设计为例

    我们需要做有依据,有逻辑,有理念的设计,需要发散思维,整合创意,严谨输出,让设计经得起推敲 前言 ​ 2018年春节已远去,一直想把Lofter新年Logo设计思路分享给大家,直到现在才整理出来,希望 ...